ActivityThread-->Android的主线程。

main方法.png H.png HcodeToString.png HhandleMessage.png

main方法通过Looper获取主线程、结束主线程。
H就是ActivityThread的Handler,主要包含了四大组件的启动和停止等过程。
通过ApplicationThread和AMS进行进程间通信,AMS以进程间通信的方式完成ActivityThread的请求后会回到ApplicationThread中的Binder方法,然后ApplicationThread会向H发送消息,H收到消息后会将ApplicationThread中的逻辑切换到ActivityThread中执行,即切换到主线程中去执行,这个过程就是主线程的消息循环模式。

Looper

子线程中用Handler的方法.png prepare.png loop.png quit.png

MessageQueue

next

    Message next() {        // Return here if the message loop has already quit and been disposed.        // This can happen if the application tries to restart a looper after quit        // which is not supported.        final long ptr = mPtr;        if (ptr == 0) {            return null;        }        int pendingIdleHandlerCount = -1; // -1 only during first iteration        int nextPollTimeoutMillis = 0;        for (;;) {            if (nextPollTimeoutMillis != 0) {                Binder.flushPendingCommands();            }            nativePollOnce(ptr, nextPollTimeoutMillis);            synchronized (this) {                // Try to retrieve the next message.  Return if found.                final long now = SystemClock.uptimeMillis();                Message prevMsg = null;                Message msg = mMessages;                if (msg != null && msg.target == null) {                    // Stalled by a barrier.  Find the next asynchronous message in the queue.                    do {                        prevMsg = msg;                        msg = msg.next;                    } while (msg != null && !msg.isAsynchronous());                }                if (msg != null) {                    if (now < msg.when) {                        // Next message is not ready.  Set a timeout to wake up when it is ready.                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);                    } else {                        // Got a message.                        mBlocked = false;                        if (prevMsg != null) {                            prevMsg.next = msg.next;                        } else {                            mMessages = msg.next;                        }                        msg.next = null;                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);                        msg.markInUse();                        return msg;                    }                } else {                    // No more messages.                    nextPollTimeoutMillis = -1;                }                // Process the quit message now that all pending messages have been handled.                if (mQuitting) {                    dispose();                    return null;                }                // If first time idle, then get the number of idlers to run.                // Idle handles only run if the queue is empty or if the first message                // in the queue (possibly a barrier) is due to be handled in the future.                if (pendingIdleHandlerCount < 0                        && (mMessages == null || now < mMessages.when)) {                    pendingIdleHandlerCount = mIdleHandlers.size();                }                if (pendingIdleHandlerCount <= 0) {                    // No idle handlers to run.  Loop and wait some more.                    mBlocked = true;                    continue;                }                if (mPendingIdleHandlers == null) {                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];                }                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);            }            // Run the idle handlers.            // We only ever reach this code block during the first iteration.            for (int i = 0; i < pendingIdleHandlerCount; i++) {                final IdleHandler idler = mPendingIdleHandlers[i];                mPendingIdleHandlers[i] = null; // release the reference to the handler                boolean keep = false;                try {                    keep = idler.queueIdle();                } catch (Throwable t) {                    Log.wtf(TAG, "IdleHandler threw exception", t);                }                if (!keep) {                    synchronized (this) {                        mIdleHandlers.remove(idler);                    }                }            }            // Reset the idle handler count to 0 so we do not run them again.            pendingIdleHandlerCount = 0;            // While calling an idle handler, a new message could have been delivered            // so go back and look again for a pending message without waiting.            nextPollTimeoutMillis = 0;        }    }
enqueueMessage.png quit.png removeAllFutureMessagesLocked.png

Message

callback.png obtain.png recycle.png recycleUnchecked.png

Handler

Handler构造方法.png Callback.png dispatchMessage.png post.png 循环.png handleMessage.png send.png sendMessageAtTime.png enqueueMessage.png

ThreadLocal

hash.png set.png initializeValues.png values.png values.put.png

Hander机制

总.png

更多相关文章

  1. android用户界面-组件Widget-网络视图WebView
  2. Android(安卓)AlertDialog 方法setView(view,0,0,0,0)开发自定义
  3. Android(安卓)进阶——Framework 核心四大组件之跨进程共享组件C
  4. AOP编程_Android优雅权限框架(2)Demo完全解析
  5. Android手势滑动实现ImageView缩放图片大小
  6. TextView自定义跑马灯效果
  7. Android(安卓)Launcher源码研究(二) 加载app流程1
  8. Android(安卓)Java生成随机数的方法
  9. Android(安卓)PathMeasure的使用详解

随机推荐

  1. Robotium 一款Android自动化测试框架
  2. Android(安卓)-- 怎么发出和接收广播, Bro
  3. Android沉浸式状态栏+图片背景+标题栏渐
  4. 分析称RIM立即转向采用谷歌Android七大理
  5. 去掉listview的分割线和分割线的颜色,高度
  6. Android智能指针sp wp详解
  7. Android五大布局特性
  8. Android开发本地及网络Mp3音乐播放器(三)
  9. Android socket网络编程要注意
  10. Android中定时器Timer和TimerTask的启动,