系统服务的启动过程是从SystemServer的main函数开始的:
public static void main(String[] args) {
new SystemServer().run();
}
该方法调用之后,run方法就会执行。run方法主要作以下工作:
...
//1、 设置系统属性,例如国家语言等
SystemProperties.set("persist.sys.timezone", "GMT");
SystemProperties.set("persist.sys.country", "");
... //2、 开启消息循环
// Prepare the main looper thread (this thread).
Looper.prepareMainLooper();
...
// Initialize the system context.
// 3、创建系统上下文
createSystemContext();
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart,
mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class,
mSystemServiceManager);
... // 4、启动各种系统服务,引导服务、核心服务、其他服务
startBootstrapServices();
startCoreServices();
startOtherServices();
...
// Loop forever.
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
1、会设置一些系统配置属性,例如语言,时区,国家等等。
2、开启消息循环。安卓是基于消息事件推动的,且是多线程的。主线程系统会帮我们创建Looper对象
3、创建上下文。
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
可以看到,实例化了ActivityThread,并通过它获取到上下文,然后设置一些系统主题样式,那么ActivityThread是如何来的呢?
public static ActivityThread systemMain() {
// The system process on low-memory devices do not get to use hardware
// accelerated drawing, since this can add too much overhead to the
// process.
if (!ActivityManager.isHighEndGfx()) {
ThreadedRenderer.disable(true);
} else {
ThreadedRenderer.enableForegroundTrimming();
}
ActivityThread thread = new ActivityThread();
thread.attach(true, 0);
return thread;
}
其实就是new了一个ActivityThread,new出来的只是单纯的一个类,很明显,它的功能职责是在attach里面被赋予的。需要注意,第一个参数设置为true,然后跟进去看看:
if (!system) {
...
// Watch for getting close to heap limit.
BinderInternal.addGcWatcher(new Runnable() {
@Override public void run() {
...
Runtime runtime = Runtime.getRuntime();
long dalvikMax = runtime.maxMemory();
long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
if (dalvikUsed > ((3*dalvikMax)/4)) {
mSomeActivitiesChanged = false;
mgr.releaseSomeActivities(mAppThread);
}
});
} else {
...
mInstrumentation = new Instrumentation();
mInstrumentation.basicInit(this);
ContextImpl context = ContextImpl.createAppContext(
this, getSystemContext().mPackageInfo);
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
}
可以看到,根据传递进来的参数做出不同处理,如果是系统服务的话,它就会1、实例化Instrumentation;2、创建上下文Context的实现类ContextImpl;3、创建Application,同时调用它的onCreate方法;如果不是就会做一些监听内存使用状态工作,如果超过最大内存的3/4,则会从ActivityRecord里面销毁一些Activity。
4、启动各种系统服务,如引导服务,核心服务,其他服务
4.1、引导服务:AMS,PMS包管理器等
4.2、核心服务:电池等
4.3、其他服务:WMS,蓝牙等
首先会实例化SystemServerManager管理类,然后调用它的startService方法。
public <T extends SystemService> T startService(Class<T> serviceClass) {
final String name = serviceClass.getName();
final T service;
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext);
startService(service);
return service;
}
public void startService(@NonNull final SystemService service) {
// Register it.
mServices.add(service);
// Start it
service.onStart();
}
可以看到通过反射先实例化一个具体服务,然后注册该服务,最后调用该服务的onStart()方法。需要注意的是注册服务是将所有的系统服务添加到一个List中进行注册,然后调用该服务的话,就从集合中去获取即可。