- 《Android Service基础》
- 《Android Service回调和配置》
- 《Android Service aidl使用及进阶》
- 《Android Service更多知识》
- 《Android 中的 IntentService 类详解》
- 《Android Service aidl分析》
- 《Android Service 流程分析》
在前面已经对Service基本使用及aidl使用及源码进行的分析。在这篇文章中,我们来看一下Android Service的启动流程和绑定流程,以便于更好的理解Android Service的机制。
启动Service过程
Android Service启动时序图
上图就是Android Service启动时序图,对照图查看代码。
我们在 Activity 中调用 startService()
方法时,直接调用了 调用 ContextWrapper
的 startService()
@Override
public ComponentName startService(Intent service) {
return mBase.startService(service);
}
mBase 就是 ContextImpl
,进入 ContextImpl
中查看
@Override
public ComponentName startService(Intent service) {
warnIfCallingFromSystemProcess();
return startServiceCommon(service, false, mUser);
}
private ComponentName startServiceCommon(Intent service, boolean requireForeground,
UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess(this);
// ActivityManager.getService() 就是获取到的 ActivityManagerService 对象
ComponentName cn = ActivityManager.getService().startService(
mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
getContentResolver()), requireForeground,
getOpPackageName(), user.getIdentifier());
// ... 省略 对 cn 进行校验
return cn;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
ActivityManager
中获取 ActivityManagerService
代码如下:
public static IActivityManager getService() {
return IActivityManagerSingleton.get();
}
// 通过 Singleton 创建IActivityManager.aidl接口对象,实现类为 `ActivityManagerService`
private static final Singleton<IActivityManager> IActivityManagerSingleton =
new Singleton<IActivityManager>() {
@Override
protected IActivityManager create() {
final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
// 由一下代码可知,IActivityManager 是aidl,也就说明Service的启动过程是一个IPC的过程
final IActivityManager am = IActivityManager.Stub.asInterface(b);
return am;
}
};
进入 ActivityManagerService
中查看 startService()
方法
@Override
public ComponentName startService(IApplicationThread caller, Intent service,
String resolvedType, boolean requireForeground, String callingPackage, int userId)
throws TransactionTooLargeException {
enforceNotIsolatedCaller("startService");
// ... 省略 Refuse possible leaked file descriptors 对service文件描述和调用包进行判断
synchronized(this) {
final int callingPid = Binder.getCallingPid();
final int callingUid = Binder.getCallingUid();
final long origId = Binder.clearCallingIdentity();
ComponentName res;
try {
// 调用 ActiveServices 的 startServiceLocked() 方法
res = mServices.startServiceLocked(caller, service,
resolvedType, callingPid, callingUid,
requireForeground, callingPackage, userId);
} finally {
Binder.restoreCallingIdentity(origId);
}
return res;
}
}
mServices 是 ActiveServices
类,该类是 AMS(ActivityManagerService
) 的辅助类,是对 Service
进行管理的工具类,包含Service的启动、绑定、解绑、销毁等过程。查看 ActiveServices
的 startServiceLocked()
方法,一下是ActiveServices
中的方法调用过程:
// ActiveServices#startServiceLocked()
ComponentName startServiceLocked(IApplicationThread caller, Intent service, String resolvedType,
int callingPid, int callingUid, boolean fgRequired, String callingPackage, final int userId)
throws TransactionTooLargeException {
// 调用 startServiceLocked()
return startServiceLocked(caller, service, resolvedType, callingPid, callingUid, fgRequired,
callingPackage, userId, false);
}
// ActiveServices#startServiceLocked()
ComponentName startServiceLocked(IApplicationThread caller, Intent service, String resolvedType,
int callingPid, int callingUid, boolean fgRequired, String callingPackage,
final int userId, boolean allowBackgroundActivityStarts)
throws TransactionTooLargeException {
// ... 省略
ServiceLookupResult res =
retrieveServiceLocked(service, null, resolvedType, callingPackage,
callingPid, callingUid, userId, true, callerFg, false, false);
// 取出 ServiceRecord 对象,后面启动过程中都是使用的它
ServiceRecord r = res.record;
// ... 省略 判断和处理前台服务
r.lastActivity = SystemClock.uptimeMillis();
r.startRequested = true; // 启动服务标识置为 true
r.delayedStop = false;
r.fgRequired = fgRequired;
r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),
service, neededGrants, callingUid));
// 调用 startServiceInnerLocked() 方法
ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting);
return cmp;
}
// ActiveServices#startServiceInnerLocked()
ComponentName startServiceInnerLocked(ServiceMap smap, Intent service, ServiceRecord r,
boolean callerFg, boolean addToStarting) throws TransactionTooLargeException {
// ... 省略
String error = bringUpServiceLocked(r, service.getFlags(), callerFg, false, false);
// ... 省略
return r.name;
}
// ActiveServices#bringUpServiceLocked()
private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg,
boolean whileRestarting, boolean permissionsReview