被zygnote启动,通过SystemManager管理android的服务(frameworks/base/services下的服务)SystemServer启动各种服务。
frameworks/base/services/java/com/android/server/SystemServer.java
/**
* This method is called from Zygote to initialize the system. This willcause the native
* services (SurfaceFlinger, AudioFlinger, etc..) to be started. Afterthat it will call back
* up into init2() to start the Android services.
*/
native public static void init1(String[] args);
public static void main(String[] args) {
if (SamplingProfilerIntegration.isEnabled()) {
SamplingProfilerIntegration.start();
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
SamplingProfilerIntegration.writeSnapshot("system_server", null);
}
}, SNAPSHOT_INTERVAL, SNAPSHOT_INTERVAL);
}
dalvik.system.VMRuntime.getRuntime().clearGrowthLimit();
// The system server has to run allof the time, so it needs to be
// as efficient as possible with itsmemory usage.
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
System.loadLibrary("android_servers");
init1(args);
}
public static final void init2() {
Slog.i(TAG, "Entered the Android systemserver!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}
}
从SystemServer的main函数开始执行。
1、 调用libandroid_servers.so库。
2、 调用init1方法,init1()是在native空间实现的,被Zygote调用来初始化系统,会启动native服务,如SurfaceFlinger,AudioFlinger, MediaPlayerService, CameraService等。
JNI native init1函数在frameworks/base/services/jni/com_android_server_SystemServer.java中实现,调用system_init.cpp中的system_init()。
3、 回调init2,启动Android中所有要用到的服务。初始化各种服务实例,加入到ServiceManager中,启动ActivityManagerService。调用ActivityManagerService.systemReady开启第一个activity,发送Intent.CATEGORY_HOME。
核心服务:
1. 启动电源管理器;
2. 创建Activity管理器
3. 启动Telephony Registry
4. 启动Package Manager
5. 设置Activity管理服务为系统进程
6. 启动Context Manager
7. 启动System Context Providers
8. 启动电池服务
9. 启动Alarm Manager
10. 启动Sensor服务;
11. 启动窗口管理器;
12. 启动蓝牙服务;
13. 启动挂载服务。
其它服务:
1. 启动状态栏服务;
2. 启动硬件服务;
3. 启动网络状态服务;
4. 启动网络连接服务;
5. 启动通知管理器;
6. 启动设备存储监视服务;
7. 启动定位管理器;
8. 启动搜索服务;
9. 启动剪切板服务;
10. 启动登记服务;
11. 启动壁纸服务;
12. 启动音频服务;
13. 启动HeadsetObserver(耳机监听);
14. 启动AdbSettingsObserver(处理adb命令)。
//建立新线程,New Service和AddService来建立服务
Thread thr = new ServerThread();
class ServerThread{
addBootEvent(new String("Android:SysServerInit_START"));
//初始化服务,创建各种服务实例,如:电源、网络、Wifi、蓝牙,USB等。
PowerManagerServicepower = null;
power= new PowerManagerService();
//初始化完成后加入到 ServiceManager中
ServiceManager.addService(Context.POWER_SERVICE, power);
//启动ActivityManagerService
ActivityManagerService.setSystemProcess();
ActivityManagerService.installSystemProviders();
// Wenow tell the activity manager it is okay to run third party
//code. It will call back into us once ithas gotten to the state
// wherethird party code can really run (but before it has actually
//started launching the initial applications), for us to complete our
//initialization.
//系统服务初始化准备就绪,执行xxx.systemReady()通知各个模块
ActivityManagerService.self().systemReady()
{
xxx.systemReady();
}
addBootEvent(new String("Android:SysServerInit_END"));
}