发起进程端:
1、ComponentName startService(Intent service)(ContextWrapper.java 开启服务);
2、ComponentName startService(Intent service)(ContextImpl.java 如果由系统进程唤起则输出log);
3、ComponentName startServiceCommon(Intent service,UserHandle user)(ContextImpl.java );
4、IActivityManager getDefault()(ActivityManagerNative.java 获取一个单例模式的ActivityManagerProxy对象);
5、ComponentName startService(IApplicationThread caller,Intent service,String resolvedType) throws RemoteException(ActivityManagerProxy.java)
Binder通信过程中,提供了一个IActivityManager服务接口,ActivityManagerProxy类与ActivityManagerService类都实现了IActivityManager接口。ActivityManagerProxy作为binder通信的客户端,ActivityManagerService作为binder通信的服务端,ActivityManagerProxy.startService()最终调用ActivityManagerService.startService()。
6、mRemote.transact()(ActivityManagerProxy.java)是binder通信的客户端发起方法,经过binder驱动,最后回到binder服务端ActivityManagerNative的onTransact()方法。
system_server端:
7、AMS和AMP可以通过IActivityManager接口相互通信,同样,ATP和ATN之间也可以分别作为客户端和服务端通过IApplicationThread相互通信。
boolean onTransact(intcode,Parcel data,Parcel reply, intflags)(AMN.java){
ComponentName cn = startService(app,service,resolvedType,callingPackage,userId);
}
8、ComponentName startService(IApplicationThread caller,Intent service,
String resolvedType,String callingPackage, intuserId)
throwsTransactionTooLargeException(AMS.java AMS extends AMN)
该方法参数说明:
caller:IApplicationThread类型,复杂处理
service:Intent类型,包含需要运行的service信息
resolvedType:String类型
callingPackage: String类型,调用该方法的package
userId: int类型,用户的id
9、ComponentName startServiceLocked(IApplicationThread caller,Intent service,String resolvedType,
int callingPid, int callingUid,String callingPackage, final intuserId)
throwsTransactionTooLargeException (AS.java ActivityService)
有一种重要的标记符callerFg, 用于标记是前台还是后台:
当发起方进程不等于Process.THREAD_GROUP_BG_NONINTERACTIVE,或者发起方为空, 则callerFg= true;
否则,callerFg= false;
10、ComponentName startServiceInnerLocked(ServiceMap smap,Intent service,ServiceRecord r,
boolean callerFg, boolean addToStarting)throws TransactionTooLargeException(AS.java)
11、String bringUpServiceLocked(ServiceRecord r, intintentFlags, booleanexecInFg,
boolean whileRestarting, boolean permissionsReviewRequired)
throws TransactionTooLargeException(AS.java)
当目标进程已存在,则直接执行realStartServiceLocked();
当目标进程不存在,则先执行startProcessLocked创建进程, 经过层层调用最后会调用到AMS.attachApplicationLocked, 然后再执行realStartServiceLocked()。
对于非前台进程调用而需要启动的服务,如果已经有其他的后台服务正在启动中,那么我们可能希望延迟其启动。这是用来避免启动同时启动过多的进程(非必须的)。
12、ProcessRecord startProcessLocked(String processName,ApplicationInfo info,
booleanknownToBeDead, intintentFlags,String hostingType,ComponentName hostingName,
booleanallowWhileBooting, booleanisolated, intisolatedUid, booleankeepIfLarge,
String abiOverride,String entryPoint,String[] entryPointArgs,Runnable crashHandler)(AMS.java)
13、boolean attachApplicationLocked(IApplicationThread thread,
int pid)(AMS.java)
14、boolean attachApplicationLocked(ProcessRecord proc,String processName)
throws RemoteException(AS.java)
15、当需要创建新进程,则创建后经历过attachApplicationLocked,则会再调用realStartServiceLocked();
当不需要创建进程, 即在[流程11]中直接就进入了realStartServiceLocked();
16、void realStartServiceLocked(ServiceRecord r,
ProcessRecord app, booleanexecInFg) throws RemoteException(ActiveService.java)
在bumpServiceExecutingLocked会发送一个延迟处理的消息SERVICE_TIMEOUT_MSG。在方法scheduleCreateService执行完成,也就是onCreate回调执行完成之后,便会remove掉该消息。但是如果没能在延时时间之内remove该消息,则会进入执行service timeout流程。
17、void bumpServiceExecutingLocked(ServiceRecord r, booleanfg,String why)(AS.java)
18、void scheduleServiceTimeoutLocked(ProcessRecord proc)(AS.java)
boolean sendMessageAtTime(Message msg, longuptimeMillis)
发送延时消息SERVICE_TIMEOUT_MSG,延时时长:
对于前台服务,则超时为SERVICE_TIMEOUT,即timeout=20s;
对于后台服务,则超时为SERVICE_BACKGROUND_TIMEOUT,即timeout=200s;
19、[继流程16]void scheduleCreateService(IBinder token,ServiceInfo info,
CompatibilityInfo compatInfo, intprocessState)throws RemoteException(ATP.java)
目标进程端
借助于ATP/ATN这对Binder对象,便完成了从system_server所在进程到Service所在进程调用过程
20、ATP的mRemote.transact()方法通过binder驱动,最终回到ATN的onTransact()方法:
boolean onTransact(intcode,Parcel data,Parcel reply, intflags)
throws RemoteException
21、void scheduleCreateService(IBinder token,
ServiceInfo info,CompatibilityInfo compatInfo, intprocessState)(ApplicationThread.java 该类是ActivityThread.java的内部类,该方法执行在ActivityThread线程)
22、void handleMessage(Message msg)(ActivityThread.java 通过handler 的sendMessage()方法和handleMessage()方法在同一进程内发送和接收信息)
23、void handleCreateService(CreateServiceData data)(ActivityThread.java)
24、service.onCreate()
最终调用Service.onCreate()方法,对于目标服务都是继承于Service,并覆写该方式,调用目标服务的onCreate()方法。拨云见日,到此总算是进入了Service的生命周期。
25、void serviceDoneExecuting(IBinder token,int type,int startId,int res)(ActivityManagerService.java)
由[流程17]的bumpServiceExecutingLocked()发送一个延时消息SERVICE_TIMEOUT_MSG
26、void serviceDoneExecutingLocked(ServiceRecord r, int type, intstart Id, int res)
(ActiveServices.java )
27、void serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying,
boolean finishing)(ActiveServices.java )
流程23执行后便会移除服务启动超时的消息SERVICE_TIMEOUT_MSG。 Service启动过程出现ANR,”executing service [发送超时serviceRecord信息]”, 这往往是service的onCreate()回调方法执行时间过长。
流程16 realStartServiceLocked方法在完成onCreate操作,解下来便是进入onStartCommand方法. 见下文.
28、void sendServiceArgsLocked(ServiceRecord r, boolean execInFg,
boolean oomAdjusted) throws TransactionTooLargeException(ActiveServices.java)
[流程16]中的AS.realStartServiceLocked的过程先后依次执行如下方法:
执行scheduleCreateService()方法,层层调用最终回调Service.onCreate();
执行scheduleServiceArgs()方法,层层调用最终回调Service.onStartCommand()。
总结: