Android-Activity启动流程-前言

回去简单看了哈Handler源码的东西,混了个眼熟,其中有个 ActivityThread的部分,这个就涉及到应用启动流程相关了...开始前我有回忆了下之前跟踪的知识,感觉过两天就忘记了,是怎么回事呀!!!

  6524
6525        Looper.prepareMainLooper(); // 主线程准备Looper对象
6526
6527        ActivityThread thread = new ActivityThread();
6528        thread.attach(false);
6529
6530        if (sMainThreadHandler == null) {
6531            sMainThreadHandler = thread.getHandler();
6532        }
6538
6541        Looper.loop(); // 开始做轮询

100    /**
101     * Initialize the current thread as a looper, marking it as an
102     * application's main looper. The main looper for your application
103     * is created by the Android environment, so you should never need
104     * to call this function yourself.  See also: {@link #prepare()}
105     */
106    public static void prepareMainLooper() {
107        prepare(false); // 创建Looper实例对象并保存到ThreadLocal sThreadLocal中;
108        synchronized (Looper.class) {
109            if (sMainLooper != null) {
110                throw new IllegalStateException("The main Looper has already been prepared.");
111            }
112            sMainLooper = myLooper(); // 获取sThreadLocal中保存的Looper实例对象;
113        }
114    }
115

129    public static void loop() {
130        final Looper me = myLooper();
131        if (me == null) {
132            throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
133        }
134        final MessageQueue queue = me.mQueue;
135
136        // Make sure the identity of this thread is that of the local process,
137        // and keep track of what that identity token actually is.
138        Binder.clearCallingIdentity();
139        final long ident = Binder.clearCallingIdentity();
140
141        for (;;) {
142            Message msg = queue.next(); // might block 没有消息则阻塞
143            if (msg == null) {
144                // No message indicates that the message queue is quitting.
145                return;
146            }
163            try {
164                msg.target.dispatchMessage(msg); // 获取到队列消息,进行事件分发;
166            } finally {
167                if (traceTag != 0) {
168                    Trace.traceEnd(traceTag);
169                }
170            }
194
195            msg.recycleUnchecked();
196        }
197    }

93    /**
94     * Handle system messages here. 处理系统消息
95     */
96    public void dispatchMessage(Message msg) {
97        if (msg.callback != null) {
98            handleCallback(msg);
99        } else {
100            if (mCallback != null) {
101                if (mCallback.handleMessage(msg)) {
102                    return;
103                }
104            }
105            handleMessage(msg);
106        }
107    }

    // Message与Handler的捆绑过程;
    public final boolean sendMessage(Message msg)
    {
        return sendMessageDelayed(msg, 0);
    }

    .....

     private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this; // 所以这个target就是 handler.sendMessage(message);的该handler本身;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }

流程,打算怎么入手了???想想看......

小萌新真的是搬砖,找了一堆链接,看人家怎么个流程,然后自己花了一天时间,先跟了一遍(不对,准确应该是搜索了一遍,O(∩_∩)O哈哈~)

直接上我跟的记录(Launcher起家、Android6.0.0_r5,其他版本不一定一样):

http://androidxref.com/6.0.0_r5/xref/packages/apps/Launcher3/src/com/android/launcher3/Launcher.java#startActivity

[图片上传中...(image-822a40-1569550513684-2)]

[图片上传中...(image-f9919d-1569550513684-1)]

    xref: /packages/apps/Launcher3/src/com/android/launcher3/Launcher.java

Step1:

1482    /**
1483     * Creates a view representing a shortcut inflated from the specified resource.
1484     *
1485     * @param parent The group the shortcut belongs to.
1486     * @param info The data structure describing the shortcut.
1487     *
1488     * @return A View inflated from layoutResId.
1489     */
1490    public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1491        BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
1492                parent, false);
1493        favorite.applyFromShortcutInfo(info, mIconCache);
1494        favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
1495        favorite.setOnClickListener(this); // 给应用快捷方式设置点击事件
1496        favorite.setOnFocusChangeListener(mFocusHandler);
1497        return favorite;
1498    }

其他Step

其他Step,点击查看

目前也是混眼熟,要搞基本比较了解还得一段时间...主要是太菜了!

网友也是这样过来的 Activity启动流程 - 掘金:

[图片上传中...(image-729205-1569550513684-0)]

附上相关比较可以的链接(主要还是自己要参考然后慢慢看,毕竟不是一天两天的事情,难鸭!):

https://blog.csdn.net/xgq330409675/article/details/78938926

https://blog.csdn.net/jiangwei0910410003/article/details/52549333

Activity启动流程 - 掘金

Android系统架构开篇 - Gityuan博客 | 袁辉辉的技术博客

Android中为什么主线程不会因为Looper.loop()里的死循环卡死?

你可能感兴趣的:(Android-Activity启动流程-前言)