Activity#runOnUiThreadメソッドで簡単スレッドハンドリングについてメモ

WS(ワーカースレッド)からUI(メインスレッド)にハンドリングする場合、Activityクラスが持つ
runOnUiThreadメソッドで簡単に実装できる

実装方法

WS→UIのサンプル

// WSコード上の中で・・・
private void test() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // UIスレッド処理
        }
    }
};

※Handlerを使ってハンドリングをすることも出来るがこちらのほうがシンプル。ただ動作仕様を確認することが必要。

動作仕様

Runs the specified action on the UI thread. If the current thread 
is the UI thread, then the action is executed immediately. 
If the current thread is not the UI thread, the action is posted 
to the event queue of the UI thread.

Google翻訳

UIスレッド上で、指定されたアクションを実行します。
現在のスレッドがUIスレッドである場合、アクションがすぐに実行されます。
現在のスレッドがUIスレッドでない場合、アクションはUIスレッドの
イベントキューにポストされている。

参考:
http://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)

その他おすすめの備忘録

Tagged with:
 

コメントを残す