service有两种工作状态,两种状态可以共存
- 启动状态,主要用于执行后台计算
- 绑定状态,用于其他组件与service的交互
(1)启动Service
/**
通过startService()来启动一个Service
**/
Intent intent = new Intent(this,MyService.class);
startService(intent);
/**
进入ContextWrapper的startService()方法,mBase类型是ContextImpl
ContextImpl在activity启动中通过attact()初始化时关联,这里的mBase就是这个ContextImpl
**/
@Override
public ComponentName startService(Intent service) {
return mBase.startService(service);
}
/**
mBase就是ContextImpl,进入到ContextImpl里面的startService()方法
ContextImpl类是ReceiverRestrictedContext里面的一个内部类
**/
@Override
public ComponentName startService(Intent service) {
warnIfCallingFromSystemProcess();
return startServiceCommon(service, mUser);
}
/**
然后再调用startServiceCommon()方法
ActivityManagerNative.getDefault()获取到的就是一个ActivityManagerService对象
**/
private ComponentName startServiceCommon(Intent service, UserHandle user) {
try {
validateServiceIntent(service);
service.prepareToLeaveProcess(this);
//通过AMS来启动服务
ComponentName cn = ActivityManagerNative.getDefault().startService(
mMainThread.getApplicationThread(), service, service.resolveTypeIfNeeded(
getContentResolver()), 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());
}
}
return cn;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
通过获取到AMS来启动服务,进入到AMS里面的startService()方法,通过ActiveServices类来处理,mServices是一个ActiveServices类,是一个辅助的启动类,里面包括了对Service的一系列操作
/**
进入到AMS里面的startService()方法
执行mServices.startServiceLocked,
mServices是一个ActiveServices类,是一个辅助的启动类,里面包括了对Service的一系列操作
**/
@Override
public ComponentName startService(IApplicationThread caller, Intent service,
String resolvedType, 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);
synchronized(this) {
final int callingPid = Binder.getCallingPid();
final int callingUid = Binder.getCallingUid();
final long origId = Binder.clearCallingIdentity();
ComponentName res = mServices.startServiceLocked(caller, service,
resolvedType, callingPid, callingUid, callingPackage, userId);
Binder.restoreCallingIdentity(origId);
return res;
}
}
/**
进入startServiceLocked()方法
执行startServiceInnerLocked()
**/
ComponentName startServiceLocked(IApplicationThread caller, Intent service, String resolvedType,
int callingPid, int callingUid, String callingPackage, final int userId)
throws TransactionTooLargeException {
...
return startServiceInnerLocked(smap, service, r, callerFg, addToStarting);
}
/**
进入startServiceInnerLocked(),跳转到bringUpServiceLocked()方法里面
ServiceRecord是一个Service记录,贯穿整个service的启动过程
**/
ComponentName startServiceInnerLocked