构造器
public SystemServiceManager(Context context) {
mContext = context;
}
startService方法
public SystemService startService(String className) {
final Class<SystemService> serviceClass;
try {
/*根据类名获取相应的Class类*/
serviceClass = (Class<SystemService>)Class.forName(className);
} catch (ClassNotFoundException ex) {
Slog.i(TAG, "Starting " + className);
throw new RuntimeException("Failed to create service " + className
+ ": service class not found, usually indicates that the caller should "
+ "have called PackageManager.hasSystemFeature() to check whether the "
+ "feature is available on this device before trying to start the "
+ "services that implement it", ex);
}
/*根据Class类,开启服务*/
return startService(serviceClass);
}
public <T extends SystemService> T startService(Class<T> serviceClass) {
final String name = serviceClass.getName();
Slog.i(TAG, "Starting " + name);
// 如果不是系统服务的子类,则抛出异常
if (!SystemService.class.isAssignableFrom(serviceClass)) {
throw new RuntimeException("Failed to create " + name
+ ": service must extend " + SystemService.class.getName());
}
final T service;
try {
/*获取类的构造器*/
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
/*创建类的实例*/
service = constructor.newInstance(mContext);
} catch (InstantiationException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service must have a public constructor with a Context argument", ex);
} catch (InvocationTargetException ex) {
throw new RuntimeException("Failed to create service " + name
+ ": service constructor threw an exception", ex);
}
/*注册服务,将对象加入到ArrayList mServices中*/
mServices.add(service);
try {
/*调用service的onStart方法,启动服务*/
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + name
+ ": onStart threw an exception", ex);
}
return service;
}
startBootPhase
调用这个方法,会调用所有注册的系统服务的onBootPhase方法。
目前看来,这个方法用来通知系统的不同的启动阶段,不同的服务根据需要,在不同的阶段做不同的任务。类似的方法还有很多,startUser方法触发系统服务的onStartUser方法,switchUser方法触发系统服务的onSwitchUser, stopUser方法触发系统服务的onStopUser,cleanupUser方法触发系统的onCleanupUser
public void startBootPhase(final int phase) {
if (phase <= mCurrentPhase) {
throw new IllegalArgumentException("Next phase must be larger than previous");
}
mCurrentPhase = phase;
Slog.i(TAG, "Starting phase " + mCurrentPhase);
final int serviceLen = mServices.size();
for (int i = 0; i < serviceLen; i++) {
final SystemService service = mServices.get(i);
try {
service.onBootPhase(mCurrentPhase);
} catch (Exception ex) {
throw new RuntimeException("Failed to boot service "
+ service.getClass().getName()
+ ": onBootPhase threw an exception during phase "
+ mCurrentPhase, ex);
}
}
}
安全模式的设置与获取
/** Sets the safe mode flag for services to query. */
public void setSafeMode(boolean safeMode) {
mSafeMode = safeMode;
}
/**
* Returns whether we are booting into safe mode.
* @return safe mode flag
*/
public boolean isSafeMode() {
return mSafeMode;
}
dump方法
dump时,打印所有注册的系统服务。
public void dump() {
StringBuilder builder = new StringBuilder();
builder.append("Current phase: ").append(mCurrentPhase).append("\n");
builder.append("Services:\n");
final int startedLen = mServices.size();
for (int i = 0; i < startedLen; i++) {
final SystemService service = mServices.get(i);
builder.append("\t")
.append(service.getClass().getSimpleName())
.append("\n");
}
Slog.e(TAG, builder.toString());
}
SystemServiceManager的启动
在SystemServer启动后,在run中创建SystemServiceManager对象。
紧接着,依赖于SystemServiceManager进行系统服务的启动。
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Start services.
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
}