Service组件研究

Android四大组件之Service

Service启动过程

1. 相关简介

ActiveServices: 管理所有services
启动过程通过context->AMS->ActiveServices实现
通过ActivityThread的applicationThread实现
process的回调
activityThread中的token为AMS中ProcessRecord的远端
activityThread中有mServices存放了该进程下的所有service,以toke binder作为key,该token为AMS对应的ServiceRecord

2. 流程

Created with Raphaël 2.1.0 ContextImpl ContextImpl ActivityManagerService ActivityManagerService ActiveServices ActiveServices ActiveService ActiveService ActivityThread ActivityThread startService startService 传入ApplicationThread和userID startServiceLocked retrieveServiceLocked 在ActiveServices mServiceMap中是否存在该service,mServiceMap根据uid保存 startServiceInnerLocked bringUpServiceLocked 如果该进程已经存在,直接调用realStartServiceLocked,否则需要通过AMS启动新的进程 startProcessLocked main attachApplication attachApplicationLocked attachApplicationLocked realStartServiceLocked scheduleCreateService handleCreateService 加载service的class信息 serviceDoneExecuting serviceDoneExecutingLocked

Service绑定过程

1. 相关简介

ServiceRecord: 描述一个正在运行的service,其中bindings描述目前绑在该service的intent集合
connections存放所有的service connection
ServiceRecord包含:
- 一个IntentBindRecord的list,每个IntentBindRecord中存放了一个intent和对应的service的binder,以及以这个intent绑定的AppBindRecord的list
- 一个ConnectionRecord的hashmap,该hashmap以ServiceConnection的binder对象为key,存放了以改binder绑定的CR对象。一个CR对象内部有一个AppBindRecord,activity以及ConnectionService对象(封装成了ServiceDispatcher)。
IntentBindRecord: intent和service和app的绑定关系,存放了intent和service的对应关系,以及绑定在该service上的所有app和process
AppBindRecord: 描述service和client的绑定关系,存放在IntentBindRecord
ConnectionRecord: 描述一个service的绑定,包含activity信息等等。描述AppBindRecord到ServiceRecord的绑定关系。即使相同的AppBindRecord(app与service的一个绑定),activity不同也算两个ConnectionRecord,考虑到同一个ServiceConnection可能被不同的activity共用

2. 流程

Created with Raphaël 2.1.0 ContextImpl ContextImpl ActivityManagerService ActivityManagerService ActiveServices ActiveServices ServiceDispatcher ServiceDispatcher ServiceRecord ServiceRecord ActivityThread ActivityThread ActivityManagerServices ActivityManagerServices (LoadApk)InnerConnection (LoadApk)InnerConnection ServiceConnection ServiceConnection bindService bindServiceCommon bindService bindServiceLocked retrieveServiceLocked new 保存了service与intent,app,Process的对应关系 retrieveAppBindingLocked 保存app信息 bringUpServiceLocked realStartServiceLocked 此时Process已经启动,AMS中能获取到该Process,直接启动Service scheduleCreateService 新启动的Process的ActivityThread handleCreateService requestServiceBindingsLocked requestServiceBindingLocked 遍历绑定每个intent scheduleBindService handleBindService publishService publishServiceLocked 发布service,IntentBindRecord记录绑定成功 connected connected sd中的该handler在bindServiceCommon设过,为ActivityThread中的mH,因此会将RunConnection传递到activityThread中执行 doConnected onServiceConnected
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值