Josco
Josco

Reputation: 3

skipped 47 frames , application may doing too much work on its main thread

This occurs while am saving a spinner value to a file. I have used thread to reduse its work. But still its not working.. please help me...

public void select(View view)
    {
        final Spinner s = (Spinner) findViewById(R.id.spinner1);
        final String FILENAME = "Username";
        new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            s.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                    try {
                        FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
                        user = s.getSelectedItem().toString();

                        fos.write(user.getBytes());

                        fos.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                    // your code here
                }

            });

        }   
    });


}

Upvotes: 0

Views: 765

Answers (2)

Biswajit
Biswajit

Reputation: 1869

This is the message show by the system when you do some heavy process(memory access) on main thread, to avoid this message you need to use AsyncTask.

Reference: http://developer.android.com/reference/android/os/AsyncTask.html

Docs for AsyncTask comes with a well explained example on how you could/should use it efficiently to benefit the most from it.

Keep in mind that if you use an emulator, having these messages that the system is skipping frames is not much of a problem. It's actually quite common. Just keep in mind that the app shouldn't be skipping more than 100 frames while using the emulator.

Upvotes: 1

eirasf
eirasf

Reputation: 89

Forget about the creating the separate thread and just let an AsyncTask do it for your you.

   s.setOnItemSelectedListener(new OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
       new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground( final Void ... params ) {
            try {
                    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
                    user = s.getSelectedItem().toString();

                    fos.write(user.getBytes());

                    fos.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            return null;
        }

        @Override
        protected void onPostExecute( final Void result ) {

        }
    }.execute();
  }

  @Override
  public void onNothingSelected(AdapterView<?> parentView) {
       // your code here
  }

});

Upvotes: 1

Related Questions