Search code examples
androidmultithreadingandroid-looperandroid-runonuithread

Android - Looper.prepare() in a Thread vs Activity.runOnUIThread()


My question is quite simple but I'm not able to find a satisfying answer. The question is: Does my thread run on UI Thread if I call Looper.prepare() at the start of my Runnable?

I know Looper is for MessageQueue and exchanging data between threads but does it make the code run on UI Thread?

Below code will explain:

@Override
public void onReceive(final Context context, Intent intent) {
if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Looper.prepare();
                // ... some code ... //
                Looper.loop();
            }
        };

        Thread thread = new Thread(runnable);
        thread.start();
    }
}

Solution

  • No, it will not make your thread run on UI thread. For more info see: Communicating with the UI Thread