Tuesday, September 8, 2015

Handler 源码分析 Android

一般而言,我们都是用子线程处理耗时操作,将获取到得数据给主线程的Handler, Looper 处理!

让我们看一看Looper:

1  Looper 
     我没有找到Activtiy 在哪里创建 Looper 的,目前只是知道跟主线程关联。

看代码:

/** * Run the message queue in this thread. Be sure to call * {@link #quit()} to end the loop. */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;
    // Make sure the identity of this thread is that of the local process,    // and keep track of what that identity token actually is.    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        Printer logging = me.mLogging;        if (logging != null) {
            logging.println(">>>>> Dispatching to " + msg.target + " " +
                    msg.callback + ": " + msg.what);        }

        msg.target.dispatchMessage(msg);
        if (logging != null) {
            logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);        }

        // Make sure that during the course of dispatching the        // identity of the thread wasn't corrupted.        final long newIdent = Binder.clearCallingIdentity();        if (ident != newIdent) {
            Log.wtf(TAG, "Thread identity changed from 0x"                    + Long.toHexString(ident) + " to 0x"                    + Long.toHexString(newIdent) + " while dispatching to "                    + msg.target.getClass().getName() + " "                    + msg.callback + " what=" + msg.what);        }

        msg.recycleUnchecked();    }
}



/** * Handle system messages here. */public void dispatchMessage(Message msg) {
    if (msg.callback != null) {
        handleCallback(msg);    } else {
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {
                return;            }
        }
        handleMessage(msg);    }
}

we have three method to deal with the handle!  

the second and third method is so easy.the first method. if you create message you can set Runnabe

Message m = Message.obtain(mHandler2, new Runnable() {
    @Override    public void run() {

    }
});

private  Handler mHandler2 = new Handler(new Handler.Callback() {
    @Override    public boolean handleMessage(Message msg) {
        return false;    }
});

private  Handler mHandler = new Handler(){

    @Override    public void handleMessage(Message msg) {
        super.handleMessage(msg);    }

};

we usualy use the third method!

So we know why. we crate handler we must set handlerMessage(){};


有一个问题,我们的主线程的Looper 是如何差ugnjainde呢?
然后我发现在

Context:
/** * Return the Looper for the main thread of the current process.  This is * the thread used to dispatch calls to application components (activities, * services, etc). * <p> * By definition, this method returns the same result as would be obtained * by calling {@link Looper#getMainLooper() Looper.getMainLooper()}. * </p> * * @return The main looper. */public abstract Looper getMainLooper();


ContextWrapper


@Overridepublic Looper getMainLooper() {
    return mBase.getMainLooper();}

终于找到了:在ActivityThread:


        Looper.prepareMainLooper();

        ActivityThread thread = new ActivityThread();
        thread.attach(false);

        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }

        if (false) {
            Looper.myLooper().setMessageLogging(new
                    LogPrinter(Log.DEBUG, "ActivityThread"));
        }


        Looper.loop();


有个问题,为什么在Android studio  搜不到ActivityThread 类呢?
在Google 的官方文档也没有呀!


No comments:

Post a Comment