zoukankan      html  css  js  c++  java
  • (1)ActivityThread分析

    1. 入口。

    曾经一直都说Activity的人口是onCreate方法。事实上android上一个应用的入口,应该是ActivityThread。和普通的java类一样,入口是一个main方法。

    public static final void main(String[] args) {
            SamplingProfilerIntegration.start();
           ……
            Looper.prepareMainLooper();
            if (sMainThreadHandler == null) {
                sMainThreadHandler = new Handler();
            }
            ActivityThread thread = new ActivityThread();
            thread.attach(false);
           ……
            Looper.loop();
           ……
            thread.detach();
            ……
            Slog.i(TAG, "Main thread of " + name + " is now exiting");
        }

    以下细致分析一下这个main方法。


    2.Looper.prepareMainLooper();

    ActivityThread事实上就是我们常常说的UI thread,也就是主线程。我们都知道主线程能够使用Handler进行异步通信,由于主线程中已经创建了Looper,而这个Looper就是在这里创建的。假设其它线程须要使用Handler通信,就要自己去创建Looper。


    3. sMainThreadHandler = new Handler();

    创建一个Handler。


    4. ActivityThread thread = new ActivityThread();

    创建ActivityThread 对象。

    ActivityThread 有几个比較重要的成员变量,会在创建ActivityThread对象时初始化。

    (1)final ApplicationThread mAppThread = new ApplicationThread();

    ApplicationThread继承自ApplicationThreadNative, 而ApplicationThreadNative又继承自Binder并实现了IApplicationThread接口。IApplicationThread继承自IInterface。这是一个非常明显的binder结构,用于于Ams通信。IApplicationThread接口定义了对一个程序(linux的进程)操作的接口。ApplicationThread通过binder与Ams通信,并将Ams的调用,通过以下的H类(也就是Hnalder)将消息发送到消息队列,然后进行对应的操作,入activity的start, stop。

    (2)final H mH = new H();

              private final class H extends Handler

    mH负责处理ApplicationThread发送到消息队列的消息,比如:

    public void handleMessage(Message msg) {
                if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + msg.what);
                switch (msg.what) {
                    case LAUNCH_ACTIVITY: {
                        ActivityClientRecord r = (ActivityClientRecord)msg.obj;

                        r.packageInfo = getPackageInfoNoCheck(
                                r.activityInfo.applicationInfo);
                        handleLaunchActivity(r, null);
                    } break;


    5. handleLaunchActivity(r, null);

    从名字中就能够看出,这里就将进行启动activity的工作了。

    方法中主要调用了这一句:

    Activity a = performLaunchActivity(r, customIntent);


    6. performLaunchActivity()

    进行了一些初始化和赋值操作后,创建activity。

    activity = mInstrumentation.newActivity(
                        cl, component.getClassName(), r.intent);

    然后调用:

    mInstrumentation.callActivityOnCreate(activity, r.state);

    这一句就会调用到acitivity的onCreate方法了,就进入了大多数应用开发的入口了。


  • 相关阅读:
    SCP-S模拟56 C题
    NOIP模拟测试22
    NOIP模拟测试21
    NOIP模拟测试20
    NOIP模拟测试19
    网络流24题
    NOIP模拟测试18
    NOIP模拟测试16
    那些年我们颓过的游戏
    csp2019游记
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4295187.html
Copyright © 2011-2022 走看看