Posts Service 源码分析
Post
Cancel

Service 源码分析

开启服务

startService()

1
2
3
4
5
6
7
8
// Context.startService() -> ContextImpl.startServiceCommon() -[IPC 到 SystemServer 进程]-> 
// AMS.startService() -> ActiveServices.startServiceLocked() -> 
// [retreiveServiceLocked(): ServiceLookupResult ->] AMS.startServiceInnerLocked() -> 
// bringUpServiceLocked(): String
// case1已经启动: -> sendServiceArgsLocked() -> ActivityThread.ApplicationThread.shcheduleServiceArgs() ->
//      ActivityThread.handleServiceArgs() -> Service.onStartCommand()
// case2未启动: -> ActivityThread.ApplicationThread.scheduleCreateService() -> 
//      ActivityThread.handleCreateService() -> Service.onCreate()

stopService()

1
2
3
4
// Context.stopService() -> ContextImpl.stopServiceCommon() -[IPC 到 SystemServer 进程]-> 
// AMS.stopService() -> ActiveServices.stopServiceLocked() ->
// ActivityThread.ApplicationThread.scheduleStopService() -> ActivityThread.handleStopService() ->
// Service.onDestroy()

开启服务小结

  • 生命周期
    • onCreate() -> onStartCommand()-> onDestroy()

绑定服务

bindService()

1
2
3
4
5
6
7
8
9
10
// Context.bindService() ->
// LoadedApk.getServiceDispatcher(): IServiceConnection -> LoadedApk.ServiceDispatcher.getIServiceConnection()
// ServiceDispatcher.InnerConnection
// ContextImpl.bindServiceCommon() -[IPC 到 SystemServer]> AMS.bindService() ->
// ActiveServices.bindServiceLocked() -> retreiveServiceLocked() -> bringUpServiceLocked() -> 
// case1 Service 已经创建:sendServiceArgsLocked() -> ActivityThread.ApplicationThread.scheduleServiceArgs() -> 
// case2 Service 未创建:realStartServiceLocked() -> ActivityThread.ApplicationThread.scheduleCreateService() ->
//      ActivityThread.handleCreateService() -> Service.onCreate()
// requestServiceBindingLocked() -> ActivityThread.ApplicationThread.scheduleBindService() ->
// ActivityThread.handleBindService() -> Service.onBind()

unbindService()

1
2
3
4
5
6
// Context.unbindService() -> ContextImpl.unbindService() -[IPC 到 SystemServer]->
// AMS.unbindService() -> ActiveServices.unbindServiceLocked() -> removeConnectionLocked() ->
// ActivityThread.ApplicationThread.scheduleUnbindService() -> ActivityThread.handleUnbindService() -> 
// Service.onUnbind() ->
// ActiveServices.bringDownServiceIfNeededLocked() -> bringDownServiceLocked() -...-> scheduleStopService() -> 
// ActivityThread.handleStopService() -> Service.onDestroy()

绑定服务小结

  • 主要提供 c/s 接口,允许组件与 Service 进行交互或者跨进程通讯
  • bind 服务的 3 种方式
    • 扩展 Binder : 同一个进程内提供服务
    • 使用 Messenger : IPC,单线程
    • 使用 aidl : IPC,多线程

service ANR

前台 service ANR

后台 service ANR

总结

This post is licensed under CC BY 4.0 by the author.