Android basics: running code in a user interface thread

In terms of running code in the user interface thread, is there a difference between:

MainActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        Log.d("UI thread", "I am the UI thread");
    }
});

or

MainActivity.this.myView.post(new Runnable() {
    public void run() {
        Log.d("UI thread", "I am the UI thread");
    }
});

and

private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {
    protected void onPostExecute(Bitmap result) {
        Log.d("UI thread", "I am the UI thread");
    }
}
+391
source share
8 answers

None of them are the same, although they will all have the same network effect.

The difference between the first and second is that if you execute the main thread of the application while executing the code, the first ( runOnUiThread()) will immediately execute Runnable. The second ( post()) always puts Runnableat the end of the event queue, even if you are already in the main thread of the application.

, , BackgroundTask, , no-op doInBackground(), , , post(). , , . AsyncTask, , onPostExecute().

+257

HPP, - :

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        Log.d("UI thread", "I am the UI thread");
    }
});
+212

Handler

new Handler().post(new Runnable() {
    @Override
    public void run() {
        // Code here will run in UI thread
    }
});
+52

Pomber , . , . , , . TextView .

TextViewUpdater textViewUpdater = new TextViewUpdater();
Handler textViewUpdaterHandler = new Handler(Looper.getMainLooper());
private class TextViewUpdater implements Runnable{
    private String txt;
    @Override
    public void run() {
        searchResultTextView.setText(txt);
    }
    public void setText(String txt){
        this.txt = txt;
    }

}

:

textViewUpdater.setText("Hello");
        textViewUpdaterHandler.post(textViewUpdater);
+17

,

private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }


    ((MainActivity)context).runOnUiThread(new Runnable() {
        public void run() {
            Log.d("UI thread", "I am the UI thread");
        }
    });

getActivity().runOnUiThread(new Runnable() {
    public void run() {
        Log.d("UI thread", "I am the UI thread");
    }
});

, ,

+4

Android P getMainExecutor():

getMainExecutor().execute(new Runnable() {
  @Override public void run() {
    // Code will run on the main thread
  }
});

Android-:

, , . , (, ..).

CommonsBlog:

getMainExecutor() , , . , Looper Executor, .

+2

hi guys this is the main question anyway i say

use handler

new Handler().post(new Runnable() {
    @Override
    public void run() {
        // Code here will run in UI thread
    }
});
+2
source
MainActivity.this.myView.post(new Runnable() {
public void run() {
    Log.d("UI thread", "I am the UI thread");
}

});

works for me. Thanks!

0
source

All Articles