zoukankan      html  css  js  c++  java
  • ActivityThreadactivity启动分析

    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方法了,就进入了大多数应用开发的入口了。 
  • 相关阅读:
    系统开发——页面跳转函数的书写
    PC 端自动化最佳方案
    access 点滴
    调用outlook发邮件
    mac安装vmware fusion后设置vmnet8上网
    Gin框架国内安装教程
    mac下一些vscode的初始化设置和使用
    Mac: 使用SDK切换gradle版本
    transfer.sh -- 使用 curl 从命令行上传文件并返回下载地址的文件分享服务(可自架服务端)
    使用ffmpeg转码,解决IDM从youtube下下来的视频在机顶盒上放不出声音问题(使用GPU加速)
  • 原文地址:https://www.cnblogs.com/bjanzhuo/p/3576105.html
Copyright © 2011-2022 走看看