Service流程

Android 7.0版本

Service生命周期

标记service状态

bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why)
\---bumpServiceExecutingLocked(r, execInFg, "create")@realStartServiceLocked
\---bumpServiceExecutingLocked(r, execInFg, "start")@sendServiceArgsLocked(ServiceRecord r, boolean execInFg, boolean oomAdjusted)
\---bumpServiceExecutingLocked(r, execInFg, "bind");@requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i, boolean execInFg, boolean rebind)
\---bumpServiceExecutingLocked(r, false, "bring down unbind")@bringDownServiceLocked(ServiceRecord r)
\---bumpServiceExecutingLocked(s, false, "unbind")@removeConnectionLocked(ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct)
\---bumpServiceExecutingLocked(r, false, "destroy")@bringDownServiceLocked(ServiceRecord r)

startService流程

startService(Intent service)@ContextImpl.java
\---startServiceCommon(Intent service, UserHandle user)@ContextImpl.java
    \---startService(IApplicationThread caller, Intent service,
            String resolvedType, String callingPackage, int userId)@ActivityManagerProxy@ActivityManagerNative.java
        \---START_SERVICE_TRANSACTION@OnTransact@ActivityManagerNative
            \---startService(IApplicationThread caller, Intent service,
                String resolvedType, String callingPackage, int userId)@ActivityManagerService.java
                \---startServiceLocked(IApplicationThread caller, Intent service, String resolvedType,
                    int callingPid, int callingUid, String callingPackage, final int userId)@ActiveServices.java
                    \---startServiceInnerLocked(ServiceMap smap, Intent service, ServiceRecord r,
                        boolean callerFg, boolean addToStarting)@ActiveServices.java
                        \---bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg,
                            boolean whileRestarting, boolean permissionsReviewRequired)@ActiveServices.java
                            \---realStartServiceLocked(ServiceRecord r, ProcessRecord app, boolean execInFg))@ActiveServices.java
                                \---scheduleCreateService(IBinder token, ServiceInfo info,
                                    CompatibilityInfo compatInfo, int processState)@ApplicationThreadProxy@ActivityManagerNative.java
                                    \---SCHEDULE_CREATE_SERVICE_TRANSACTION@OnTransact@ActivityManagerNative.java
                                        \---scheduleCreateService(IBinder token,
                                            ServiceInfo info, CompatibilityInfo compatInfo, int processState)@ActivityThread.java
                                            \---CREATE_SERVICE@handleMessage@H@ActivityThread.java
                                                \---handleCreateService(CreateServiceData data)@ActivityThread.java
                                                    \---service.onCreate()
                                                    \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerProxy@ActivityManagerNative.java
                                                        \---SERVICE_DONE_EXECUTING_TRANSACTION@OnTransact@ActivityManagerNative.java
                                                            \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerService.java
                                                                \---serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res)@ActiveServices.java
                                                                    \---serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying,
            boolean finishing)@ActiveServices.java

启动onStartCommand流程
onStartCommand

sendServiceArgsLocked(ServiceRecord r, boolean execInFg, boolean oomAdjusted)@ActiveServices.java
\---scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId, int flags, Intent args)@ApplicationThreadProxy@ApplicationThreadNative.java
    \---SCHEDULE_SERVICE_ARGS_TRANSACTION@OnTransact@ApplicationThreadNative.java
        \---scheduleServiceArgs(IBinder token, boolean taskRemoved, int startId, int flags ,Intent args)@ApplicationThread@ActivityThread.java
            \---SERVICE_ARGS@handleMessage@H@ActivityThread.java
                \---handleServiceArgs(ServiceArgsData data)@ActivityThread.java
                    \---service.onStartCommand
                    \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerProxy@ActivityManagerNative.java
                        \---SERVICE_DONE_EXECUTING_TRANSACTION@OnTransact@ActivityManagerNative.java
                            \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerService.java
                                \---serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res)@ActiveServices.java
                                    \---serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying,
            boolean finishing)@ActiveServices.java

stopService流程

stopService(Intent service)@ContextImpl.java
\---stopServiceCommon(Intent service, UserHandle user)@ContextImpl.java
    \---stopService(IApplicationThread caller, Intent service,
            String resolvedType, int userId)@ActivityManagerProxy@ActivityManagerNative.java
            \---STOP_SERVICE_TRANSACTION@onTransact@ActivityManagerNative.java
                \---stopService(IApplicationThread caller, Intent service, String resolvedType, int userId)@ActivityManagerService.java
                    \---stopServiceLocked(IApplicationThread caller, Intent service, String resolvedType, int userId)@ActiveServices.java
                        \---stopServiceLocked(ServiceRecord service)@ActiveServices.java
                            \---bringDownServiceIfNeededLocked(ServiceRecord r, boolean knowConn, boolean hasConn)@ActiveServices.java
                                \---bringDownServiceLocked(ServiceRecord r)@ActiveServices.java
                                    \---scheduleStopService(IBinder token)@ApplicationThreadProxy@ApplicationThreadNative.java
                                        \---SCHEDULE_STOP_SERVICE_TRANSACTION@onTransact@ApplicationThreadNative.java
                                            \---scheduleStopService(IBinder token)@ApplicationThread@ActivityThread.java
                                                \---STOP_SERVICE@handleMessage@H@ActivityThread.java
                                                    \---handleStopService(IBinder token)@ActivityThread.java
                                                        \---service.onDestroy()
                                                        \---scheduleFinalCleanup(String who, String what)@ContextImpl.java
                                                            \---scheduleContextCleanup(ContextImpl context, String who, String what)@ActivityThread.java
                                                                \---CLEAN_UP_CONTEXT@handleMessage@H@ActivityThread.java
                                                                    \---performFinalCleanup(String who, String what)@ComtextImpl.java
                                                                        \---unregisterReceiver(IIntentReceiver receiver)---后面分析
                                                                        \---unbindService(IServiceConnection connection)@ActivityManagerProxy@ActivityManagerNative.java
                                                                            \---UNBIND_SERVICE_TRANSACTION@onTransact@ActivityManagerNative.java
                                                                                \---unbindService(IServiceConnection connection)@ActivityManagerService.java
                                                                                    \---unbindServiceLocked(IServiceConnection connection)@ActiveServices.java
                                                                                        \---removeConnectionLocked(
        ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct)@ActiveServices.java
        \---scheduleUnbindService(IBinder token, Intent intent)ApplicationThreadProxy@ApplicationThreadNative.java
            \---SCHEDULE_UNBIND_SERVICE_TRANSACTION@onTransact@ApplicationThreadNative.java
                \---scheduleUnbindService(IBinder token, Intent intent)@ApplicationThread@ActivityThread.java
                    \---UNBIND_SERVICE@handleMessage@H@ActivityThread.java
                        \---handleUnbindService(BindServiceData data)@ActivityThread.java
                            \---service.onUnbind()
                            \---unbindFinished(IBinder token, Intent intent, boolean doRebind)@ActivityManagerProxy@ActivityManagerNative.java==doRebind==true
                                \---UNBIND_FINISHED_TRANSACTION@onTransact@ActivityManagerNative.java
                                    \---unbindFinished(IBinder token, Intent intent, boolean doRebind)@ActivityManagerService.java
                                        \---unbindFinishedLocked(ServiceRecord r, Intent intent, boolean doRebind)@ActiveServices.java
                                            \---requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i, boolean execInFg, boolean rebind)@ActiveServices.java
                                                \---重绑定服务,分析停止
                            \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerProxy@ActivityManagerNative.java==doRebind==false
                                \---SERVICE_DONE_EXECUTING_TRANSACTION@OnTransact@ActivityManagerNative.java
                                    \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerService.java
                                        \---serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res)@ActiveServices.java
                                            \---serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying, 
            boolean finishing)@ActiveServices.java

bindService流程

bindService(Intent service, ServiceConnection conn, int flags)@ContextImp.java
\---bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler handler, UserHandle user)@ContextImp.java
    \---bindService(IApplicationThread caller, IBinder token,
            Intent service, String resolvedType, IServiceConnection connection,
            int flags,  String callingPackage, int userId)@ActivityManagerProxy@ActivityManagerNative.java
        \---BIND_SERVICE_TRANSACTION@onTransact@ActivityManagerNative.java
            \---bindService(IApplicationThread caller, IBinder token, Intent service,
                String resolvedType, IServiceConnection connection, int flags, String callingPackage,
                int userId)@ActivityManagerService.java
                \---bindServiceLocked(IApplicationThread caller, IBinder token, Intent service,
                    String resolvedType, final IServiceConnection connection, int flags,
                    String callingPackage, final int userId)@ActiveServices.java
                    创建ConnectionRecord,准备建立连接
                    \---requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i, boolean execInFg, boolean rebind)@ActiveServices.java
                        \---scheduleBindService(IBinder token, Intent intent, boolean rebind, int processState)@ApplicationThreadProxy@ApplicationThreadNative.java
                            \---SCHEDULE_BIND_SERVICE_TRANSACTION@onTransact@ApplicationThreadNative.java
                                \---scheduleBindService(IBinder token, Intent intent, boolean rebind, int processState)@ApplicationThread@ActivityThread.java
                                    \---BIND_SERVICE@handleMessage@H@ActivityThread.java
                                        \---handleBindService(BindServiceData data)@ActivityThread.java
                                            \---service.onBind()----非rebind
                                            \---publishService(IBinder token, Intent intent, IBinder service)@ActivityManagerProxy@ActivityManagerNative.java
                                                \---PUBLISH_SERVICE_TRANSACTION@onTransact@ActivityManagerNative.java
                                                    \---publishService(IBinder token, Intent intent, IBinder service)@ActivityManagerService.java
                                                        \---publishServiceLocked(ServiceRecord r, Intent intent, IBinder service)@ActivieServices.java
                                                            \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerProxy@ActivityManagerNative.java
                                                                \---SERVICE_DONE_EXECUTING_TRANSACTION@OnTransact@ActivityManagerNative.java
                                                                    \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerService.java
                                                                        \---serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res)@ActiveServices.java
                                                                            \---serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying, 
            boolean finishing)@ActiveServices.java

unbindService流程

unbindService(ServiceConnection conn)@ContextImpl.java
\---unbindService(IServiceConnection connection)@ActivityManagerProxy@ActivityManagerNative.java
    ---UNBIND_SERVICE_TRANSACTION@onTransact@ActivityManagerNative.java
         \---unbindService(IServiceConnection connection)@ActivityManagerService.java
            \---unbindServiceLocked(IServiceConnection connection)@ActiveServices.java
                \---removeConnectionLocked(ConnectionRecord c, ProcessRecord skipApp, ActivityRecord skipAct)@ActiveServices.java
                    \---scheduleUnbindService(IBinder token, Intent intent)ApplicationThreadProxy@ApplicationThreadNative.java
                        \---SCHEDULE_UNBIND_SERVICE_TRANSACTION@onTransact@ApplicationThreadNative.java
                            \---scheduleUnbindService(IBinder token, Intent intent)@ApplicationThread@ActivityThread.java
                                \---UNBIND_SERVICE@handleMessage@H@ActivityThread.java
                                    \---handleUnbindService(BindServiceData data)@ActivityThread.java
                                        \---service.onUnbind()
                                            \---unbindFinished(IBinder token, Intent intent, boolean doRebind)@ActivityManagerProxy@ActivityManagerNative.java==doRebind==true
                                                \---UNBIND_FINISHED_TRANSACTION@onTransact@ActivityManagerNative.java
                                                    \---unbindFinished(IBinder token, Intent intent, boolean doRebind)@ActivityManagerService.java
                                                        \---unbindFinishedLocked(ServiceRecord r, Intent intent, boolean doRebind)@ActiveServices.java
                                                            \---requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i, boolean execInFg, boolean rebind)@ActiveServices.java
                                                            \---重绑定服务,分析结束
                                                    \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerProxy@ActivityManagerNative.java==doRebind==false
                                                        \---SERVICE_DONE_EXECUTING_TRANSACTION@OnTransact@ActivityManagerNative.java
                                                            \---serviceDoneExecuting(IBinder token, int type, int startId, int res)@ActivityManagerService.java
                                                                \---serviceDoneExecutingLocked(ServiceRecord r, int type, int startId, int res)@ActiveServices.java
                                                                    \---serviceDoneExecutingLocked(ServiceRecord r, boolean inDestroying, 
            boolean finishing)@ActiveServices.java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值