Android Service的启动过程

来源:互联网 发布:不列颠空战 知乎 编辑:程序博客网 时间:2024/06/06 03:52

在讲service启动之前先讲两个概念:

1、context、contextWrapper、contextImpl的关系

图片

ContextWrapper、ContextImpl都是继承自Context,像我们调用startService()就是调用ContextWrapper里面mBase.startService()  mBase是ContextWrapper的实例,所以startService()的真正实现是在ContextWrapper里面的

2、ActivityManagerNative、ActivityManagerProxy、ActivityManagerService的关系

其实这是一个aidl,ActivityManagerNative、ActivityManagerProxy的实现是跟我们写***.aidl文件之后系统自动生成的一样,我们普通调用ActivityManagerNative.getDefault.startService(),其实具体实现就是在ActivityManagerService().startService()


Service的启动有两种方式:StartService()和BindService()两种

1、startService()的启动方式:

(ContextWrapper)startService()->(ContextImpl)StartService()->startServiceCommon()  接下来就是进入到ActivityManagerService里面了

service


特别提一下在(ActiveService)bringUpServiceLocked()里面会判断这个Service是否已经启动了 如果启动了就调用startServiceCommon()->... onStartCommand() 如果没有启动就调用HandleCreateService()->...onCreate()

至此:startService()就启动了


2、bindService()的启动过程:

(ContextWrapper)bindService()->(ContextImpl)bindService()->bindServiceCommon()接下来就到ActivityManagerService里面了


先判断service是否已经启动了。启动了则不回调onCreate(),启动成功之后再回调connectSuccess,之后再回调onbind()

原创粉丝点击