浅谈桌面启动一个应用Activity

   startActivity



    1、Launcher进程

    Handler机制在主线程循环处理消息,在onCreate时调用了startActivity,通过Binder进程间通信机制发送了START_ACTIVITY_TRANSATION。

    START_ACTIVITY_TRANSATION:异步请求,发送完Launcher进程睡觉等待在主线程。


    2、SystemServer

    SystemServer线程向Launcher主线程发送SCHEDULE_PAUSE_ACTIVITY_TRANSACTION(通过ApplicationThreadProxy),之所以选择Launcher主线程,是因为Launcher主线程正在等待SystemServer的返回数据。这个过程是同步的,只发送不等待返回结果。

    之后SystemServer线程向Launcher主线程发送了START_ACTIVITY_TRANSATION的返回结果。


    3、Launcher进程

    Launcher进程此时需要处理两个返回命令,一个是BR_TRANSATION(发送SCHEDULE_PAUSE_ACTIVITY_TRANSACTION),一个是BR_REPLY(START_ACTIVITY_TRANSATION的返回结果)。

status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)  
{  
    int32_t cmd;  
    int32_t err;  
  
    while (1) {  
        if ((err=talkWithDriver()) < NO_ERROR) break;  
        err = mIn.errorCheck();  
        if (err < NO_ERROR) break;  
        if (mIn.dataAvail() == 0) continue;  
          
        cmd = mIn.readInt32();//读取了BR_TRANSACTION_COMPLETE协议,mIn中就没有数据了  
        .....  
  
        switch (cmd) {  
        case BR_TRANSATION:  
            ....  
            break; 
        case BR_REPLY:
        	....
        	break; 
        .....  
    }  
    ........  
    return err;  
}

    先处理BR_TRANSATION,向主线程的Handler发送了H.PAUSE_ACTIVITY,此时由于onCreate在主线程中执行,所以这个命令还不能马上执行。

    再处理BR_REPLY,退出onCreate,继续执行Handler里面的内容H.PAUSE_ACTIVITY。向SystemServer发送ACTIVITY_PAUSED_TRANSACTION,异步请求,要求等待返回值。主线程继续睡眠等待返回值。


    4、SystemServer

    SystemServer线程会执行Process.start,来请求Zygote fork出一个新的进程,新进程入口地址ActivityThread.main。然后SystemServer继续运行,给Launcher主线程发送返回值。


    5、应用程序进程

    从入口ActivityThread.main继续执行,此时子线程用来处理binder进程间通信,在handleChildProc-->zygoteInit已经建立了。但是此时Handler主线程循环机制并未建立。

public final class ActivityThread {  
    ......  
  
    public static final void main(String[] args) {  
        SamplingProfilerIntegration.start();  
  
        Process.setArgV0("<pre-initialized>");  
  
        Looper.prepareMainLooper();  
        if (sMainThreadHandler == null) {  
            sMainThreadHandler = new Handler();  
        }  
  
        ActivityThread thread = new ActivityThread();  
        thread.attach(false);  
  
        if (false) {  
            Looper.myLooper().setMessageLogging(new  
                LogPrinter(Log.DEBUG, "ActivityThread"));  
        }  
        Looper.loop();  
  
        if (Process.supportsProcesses()) {  
            throw new RuntimeException("Main thread loop unexpectedly exited");  
        }  
  
        thread.detach();  
        String name = (thread.mInitialApplication != null)  
            ? thread.mInitialApplication.getPackageName()  
            : "<unknown>";  
        Slog.i(TAG, "Main thread of " + name + " is now exiting");  
    }  
  
    ......  
}  
     应用程序主线程向SystemServer发送ATTACH_APPLICATION_TRANSACTION,异步请求,在主线程睡眠等待。

    6、SystemServer

    SystemServer有一个线程来处理进程间通信请求,通过ApplicationTheadProxy向应用程序主线程发送BIND_APPLICATION_TRANSATION和SCHDULE_LAUNCH_ACTIVITY_TRANSATION同步请求。之所有选择应用程序主线程,是因为他正在等待SystemServer的返回数据,不用白不用。

    之后再想应用程序主线程发送返回数据。


    7、应用程序进程

    应用程序主线程被唤醒,需要处理两个BR_TRANSATION,一个BR_REPLY。

status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)  
{  
    int32_t cmd;  
    int32_t err;  
  
    while (1) {  
        if ((err=talkWithDriver()) < NO_ERROR) break;  
        err = mIn.errorCheck();  
        if (err < NO_ERROR) break;  
        if (mIn.dataAvail() == 0) continue;  
          
        cmd = mIn.readInt32();//读取了BR_TRANSACTION_COMPLETE协议,mIn中就没有数据了  
        .....  
  
        switch (cmd) {  
        case BR_TRANSATION:  
            ....  
            break; 
        case BR_REPLY:
        	....
        	break; 
        .....  
    }  
    ........  
    return err;  
}
    先处理两个BR_TRANSATION,分别向主线程Handler发送两个消息,由于目前Handler机制尚未建立完成,待处理完BR_REPLY主线程继续执行Looper.loop时,会执行。也就是应用程序进程的Application类的onCreate的方法和Activity类的onCreate方法会执行。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值