一 前言
Service组件是Android应用四大组件之一,主要用来处理与用户界面无关的逻辑。Service的启动过程与Activity的启动过程是类似的。和Activity一样,启动过程由ActivityManagerService来管理。我们下面分析的是启动的Service和启动者(可以是Activity、Service等)是在同一个进程,在此假设是在Activity启动的。
也可以复习一下Android组件管理框架—视图容器Activity之启动流程(Android P)。
二 图示调用流程
三 代码具体流程
1 frameworks/base/core/java/android/content/ContextWrapper.java
@Override
public ComponentName startService(Intent service) {
return mBase.startService(service);
}
}
这里mBase变量是ContextImpl类型,在创建activity时new 的一个ContextImpl对象,赋值给activity。
2 frameworks/base/core/java/android/app/ContextImpl.java
@Override
public ComponentName startService(Intent service) {
warnIfCallingFromSystemProcess();
return startServiceCommon(service, false, mUser);
}
}
接着看startServiceCommon。
private ComponentName startServiceCommon(Intent service, boolean requireForeground,
UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess(this);
ComponentName cn = ActivityManager.getService().startService(
mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
getContentResolver()), requireForeground,
getOpPackageName(), user.getIdentifier());
if (cn != null) {
if (cn.getPackageName().equals("!")) {
throw new SecurityException(
"Not allowed to start service " + service
+ " without permission " + cn.getClassName());
} else if (cn.getPackageName().equals("!!")) {
throw new SecurityException(
"Unable to start service " + service
+ ": " + cn.getClassName());
} else if (cn.getPackageName().equals("?")) {
throw new IllegalStateException(
"Not allowed to start service " + service + ": " + cn.getClassName());
}
}
return cn;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
ActivityManager.getService().startService,直接返回的是ActivityManagerService的实例,这一块和startActivity的过程一样,具体就不展开说了,可以参考之前的startActivity的启动流程,Android组件管理框架—视图容器Activity之启动流程(Android P)。
3 frameworks/base/core/java/android/app/ActivityManager.java
4 frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
调用到系统进程(System_server)来了。
@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
if (service != null && service.hasFileDescriptors() == true) {
throw new IllegalArgumentException("File descriptors passed in Intent");
}
if (callingPackage == null) {
throw new IllegalArgumentException("callingPackage cannot be null");
}
if (DEBUG_SERVICE) Slog.v(TAG_SERVICE,
"*** startService: " + service + " type=" + resolvedType + " fg=" + requireForeground);
synchronized(this) {
final int callingPid = Binder.getCallingPid();
final int callingUid = Binder.getCallingUid();
final long origId = Binder.clearCallingIdentity();
ComponentName res;
try {
res = mServices.startServiceLocked(caller, service,
resolvedType, callingPid, callingUid,
requireForeground, callingPackage, userId);
} finally {
Binder.restoreCallingIdentity(origId);
}
return res;
}
}
接着看mServices.startServiceLocked,mServices是ActiveService的实例。
5 frameworks/base/services/core/java/com/and