1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android Handler Looper と runOnUiThread の比較

Posted at

UI スレッド以外のスレッドから呼び出す時は、下記2つの書き方は同じものです。

Looper.getMainLooper()

Runnable task = getTask();
new Handler(Looper.getMainLooper()).post(task);

Activity#runOnUiThread()
Runnable task = getTask();
runOnUiThread(task);

UIスレッドから呼び出す時は、下記のソースにより、runOnUiThreadの場合は直接UIスレッドに実行されます。

public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);
    } else { //UIスレッドの場合は直接実行する
        action.run();
    }
}
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?