ActivityThread 源码笔记(1)

  1. ActivityThread:虽然名字是Thread,但是本身并不继承自任何一个Thread类,其Thread的功能是通过内部维护的handler(mH)实现的.

  2. ActivityThread的static main函数(入口):

    • 前面是一系列的初始化:
    • 重要的初始化: Looper.prepareMainLooper(),为当前线程配置一个Looper并放在Looper类的相应的ThreadLocal中. 为了能讲当前线程作为一个handlerThread使用.
    • new 一个 ActivityThread(thread)并接着调用attach(false);
    • sMainThreadHandler设为thread的getHandler()
    • 初始化AsyncTask类.
    • Looper.loop()开始工作,理论上将永远循环再次函数,如果此函数结束,会直接抛出一个RuntimeException(“Main thread loop unexpectedly exited”)
  3. 和static main相对的:(public static ActivityThread systemMain()):

    • new 一个 ActivityThread 并attach(true)然后返回.
    • 该函数会被ActivityManagerService和UserManagerService调用.
  4. H类: 每个ActivityThread对象都有一个H对象(mH):

    • H 类其实就是自定义的Handler类.
    • 自定了handleMessage(Message msg)函数,会根据不同的msg执行相应操作
    • 这些msg包含的操作包括:
      • 对某个Activity的操作(launch/stop/pause…)
      • 对某个Service的操作
      • 对Applocation的操作
      • 对provider的操作
      • show/hide window
    • mH所依附的线程就是创建它的线程.就是ActivityThread对应的线程.
  5. ApplicationThread类: 每一个ActivityThread都有一个ApplicationThread(mAppThread):

    • ApplicationThread extends ApplicationThreadNative
    • ApplicationThreadNative extends Binder implements IApplicationThread
    • IApplicationThread的注释(可以看到其实就是application给activity manager的一个RPC对象, activity manager的指令通过binder机制下达到此对象,此对象做相应操作):

    System private API for communicating with the application. This is given to the activity manager by an application when it starts up, for the activity manager to tell the application about things it needs to do.

  6. 里面有一堆的scheduleXXXActivity,就是对某个Activity对象做操作(stop/pause…, 会有一个IBinder token作为对Activity对象的标示),而ApplicationThread也没有更多操作,仅仅是简单的将此操作放在ActivityThread的操作队列mH中(queueOrSendMessage(…))
  7. scheduleLaunchActivity(…): 创建并运行一个Activity:
    • 会有一堆参数传递过来,包括:
      • intent(启动Activity的Intent)
      • curConfig(启动此Activity时的Configuration)
      • IBinder token(用来标示activity对象以及远端通讯用的binder)
      • ……….
    • 这些参数都会被封装在new出来的ActivityClientRecord中,除了curConfig外, 这个curConfig会作为ActivityThread的mPendingConfiguration.
    • 最后以H.LAUNCH_ACTIVITY作为msg结合new的ActivityClientRecord对象将这个task放在mH的执行队列中.
  8. handleLaunchActivity(ActivityClientRecord r, Intent customIntent):

    • 是在mH收到LAUNCH_ACTIVITY以后调用的函数, 在这种情况下,customIntent = null.
    • unscheduleGcIdler(), 取消之前
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值