AndroidサブスレッドによるUI修正方法の比較

17256 ワード

AndroidサブスレッドによるUI修正方法の比較
Androidの開発では、サブスレッドでデータを取得したり、データを処理したりしてUI表示を変更することが多いが、UIを変更するには一般的に4つの方法がある.Handler()のhandleMessage()とhandler.sendMessage(msg) 2.handler.(runnable)とhandler.postDelayed(runnable,milliseconds) 3.activity.runOnUiThread(); 4.View.いいか?postDelayed(runnable,milliseconds);
げんり
1.Handler()のhandleMessage()とhandler.sendMessage(msg)
使用時にHandlerオブジェクトを作成するには、次の手順に従います.
private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

サブスレッドでhandlerがmessageを送信する
new Thread(){
            @Override
            public void run() {
                super.run();
                Message message = Message.obtain();
                handler.sendMessage(message);
            }
        }.start();

sendMessage()メソッドは最終的にHandlerを呼び出す.EnqueueMessage()メソッドMessageをMessage Queueに入れます.
public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }

public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }

public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        if (queue == null) {
            RuntimeException e = new RuntimeException(
                    this + " sendMessageAtTime() called with no mQueue");
            Log.w("Looper", e.getMessage(), e);
            return false;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
    }

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

    boolean enqueueMessage(Message msg, long when) {
            ........

            msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            if (p == null || when == 0 || when < p.when) {
                // New head, wake up the event queue if blocked.
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                // Inserted within the middle of the queue.  Usually we don't have to wake
                // up the event queue unless there is a barrier at the head of the queue
                // and the message is the earliest asynchronous message in the queue.
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

        return true;
    }

LooperループでLooperのnextメソッドによりMessageを取り出し、msgを呼び出す.target.dispatchMessage(msg);メッセージを配布します.
 public static void loop() {
        final Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
        }
        final MessageQueue queue = me.mQueue;

        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();

        for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            // This must be in a local variable, in case a UI event sets the logger
            final Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }

            final long traceTag = me.mTraceTag;
            if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
                Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
            }
            try {
                msg.target.dispatchMessage(msg);
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }

            .......
        }
    }

msg.targetはHandlerの一例であり、dispatchMessage()はHandlerの方法である.
まずmsgを判断する.callbackがnullでない場合はhandleCallback(msg)を実行します.
public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }

callbackはhandlerを通過するときRunnableオブジェクトです.postメソッドがrunnableを送信するとこのmsgにカプセル化され、handleCallback(msg)メソッドがRunnableのrunメソッドを直接呼び出します
private static void handleCallback(Message message) {
        message.callback.run();
    }

mCallbackはCallbackインタフェースオブジェクトであり、Handlerを構築する際にCallbackオブジェクトを渡すことができ、handleMessage()メソッドを実装し、handleMessage()がtrueを返すとHandlerのhandleMessage()メソッドを実行しない
public interface Callback {
        public boolean handleMessage(Message msg);
    }
private Handler mHandler = new Handler(new Handler.Callback() {  

    @Override  
    public boolean handleMessage(Message msg) {  
        return false;  
    }  
}); 

以上がHandlerを呼び出すsendMessage()メソッドの実行フローである.
2.handler.(runnable)とhandler.postDelayed(runnable,milliseconds)
handler.post()とhandler.postDelayed()メソッドはsendMessageDelayed()メソッドを呼び出します.最後にHandlerのenqueueMessage()でMessageをMessage Queneに入れます.getPostMessage()メソッドは、新しいMessageがRunnableをMessageのcallbackに渡して最後に実行するときに、callbackがnullでないと判断したときにRunnableを実行するrunメソッドを作成します.
public final boolean post(Runnable r)
    {
       return  sendMessageDelayed(getPostMessage(r), 0);
    }
private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
    }

3.activity.runOnUiThread(); 現在のスレッドがプライマリスレッドかどうかを取得し、プライマリスレッドであればRunnableのrunメソッドを直接実行します.メインスレッドではなくHandler post(Runnable)形式でMessageをMesageQueneに入れ,最後にHandlerのdispatchMessage法でRunnableのrun法を実行する
    public final void runOnUiThread(Runnable action) {
        if (Thread.currentThread() != mUiThread) {
            mHandler.post(action);
        } else {
            action.run();
        }
    }

4.View.いいか?postDelayed(runnable,milliseconds);
この方式はまずattachInfoがnullであるかどうかを判断します.nullでない場合はattachInfoが呼び出されます.mHandler.post(runnable);AttachInfoのmHandlerはViewRootHandlerオブジェクトであり、ViewRootHandlerはHandlerのサブクラスである.最後にhandlerによってrunnableのrunメソッドを呼び出す.attachInfoがnullの場合getRunQueue()を呼び出す.post(action);
public boolean post(Runnable action) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.post(action);
        }

        // Postpone the runnable until we know on which thread it needs to run.
        // Assume that the runnable will be successfully placed after attach.
        getRunQueue().post(action);
        return true;
    }
public boolean postDelayed(Runnable action, long delayMillis) {
        final AttachInfo attachInfo = mAttachInfo;
        if (attachInfo != null) {
            return attachInfo.mHandler.postDelayed(action, delayMillis);
        }

        // Postpone the runnable until we know on which thread it needs to run.
        // Assume that the runnable will be successfully placed after attach.
        getRunQueue().postDelayed(action, delayMillis);
        return true;
    }

AttachInfoって何?AttachInfoはこのクラス名を見て、彼がバインドされた情報を代表していることを知っています.AttachInfoの中の情報は、ViewとWindowの間の情報です.ウィンドウに追加された各ビューには、ビューのdiapatchAttachedTowWindowを介してビューに配布されるAttachInfoが表示されます.ビューグループであれば、このAttachInfoもすべてのサブビューに参照で配布されます.
void dispatchAttachedToWindow(AttachInfo info, int visibility) {
        mAttachInfo = info;
        if (mOverlay != null) {
            mOverlay.getOverlayView().dispatchAttachedToWindow(info, visibility);
        }

        .....
    }

attachInfo==null説明viewはまだwindowに表示されていませんが、Viewがまだwindowにattachされていない場合はrunnableをViewRootImplのRunQueueに入れます.ではpostからRunQueueのrunnableはいつ実行されるのか、なぜViewがwindowにattachしていないときにRunQueueにpostする必要があるのか.
ViewpostとHandler postの違い
実際、Viewがwindowにattachした場合、両者は区別されず、UIスレッドを呼び出したHandlerがrunnableをMessageQueueに送信し、最後にhandlerがメッセージの配布処理を行う.
しかし、Viewがwindowにまだattachされていない場合、runnableはView RootImpl#RunQueueに置かれ、最終的には処理されますが、MessageQueueではありません.ViewRootImpl#RunQueueソースのコメントは次のとおりです.
/**
 * The run queue is used to enqueue pending work from Views when no Handler is
 * attached.  The work is executed during the next call to performTraversals on
 * the thread.
 * @hide
 */

ビューツリーがまだwindowにattachされていない場合、ビューツリー全体にHandlerは存在しません(実際には自分でnewできますが、ここではhandlerはAttachInfoの中にあります)、RunQueueでrunnableタスクの実行を遅延させ、runnableは最終的にMessageQueueに追加されず、Looperによって実行されません.ViewRootImplの次のperformTraversalsまで待っている間に、RunQueueのすべてのrunnableを取り出して実行し、RunQueueを空にします.
このようにRunQueueの役割はMessageQueueに似ているが,この中のすべてのrunnableの最後の実行タイミングは,次のperformTraversalsが到来したときであり,MessageQueueのメッセージ処理は次のloopが到来したときである.ViewRootImpl#performTraversals:
private void performTraversals() {

    // ....

    // Execute enqueued actions on every traversal in case a detached view enqueued an action
    getRunQueue().executeActions(mAttachInfo.mHandler);

    // ....
}

void executeActions(Handler handler) {
     synchronized (mActions) {
     final ArrayList actions = mActions;
     final int count = actions.size();

     for (int i = 0; i < count; i++) {
         final HandlerAction handlerAction = actions.get(i);
         handler.postDelayed(handlerAction.action, handlerAction.delay);
      }

        actions.clear();
   }
}

つまり、Viewがattachからwindowに移行していない場合、最後のrunnableの処理はMessageQueueではなく、View RootImpl自身が次のperformTraversalsに到来したときに実行される.最後はhandlerpostDelayed(handlerAction.action, handlerAction.delay);run()メソッドを実行します.