前言:
看源码也有一段时间了,为了能更深刻的理解Android源码,我准备把看过的源码用自己的思维捋一遍,通过写博客逼迫自己认真去理解.源码太庞大了,尽量能抛开繁琐的源代码把他们的功能讲出来.每次只讲某一段逻辑的源码实现,这样读起来就不会太累.
Android系统启动的第一个进程是init进程,接着init通过解析init.rc依次启动ServiceManager进程,Zygote进程,SystemServer进程.
SystemServer就是AMS,PMS,WMS等我们经常接触到的主要服务的宿主进程.
SystemServer开始启动各种服务,入口函数为main
SystemServer:
public static void main(String[] args) {
new SystemServer().run();
}
run里面就开启了大部分的Service.
startBootstrapServices();
startCoreServices();
startOtherServices();
SystemServer:
private void run() {
...
try {
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();
SystemServerInitThreadPool.shutdown();
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
traceEnd();
}
...
}
startBootstrapServices()里就开启了常见的几个Service,比如ActivityManagerService,PowerManagerService等等
startOtherServices()启动了PackageManagerService,VibratorService,AlarmManagerService,WindowManagerService等常见service
SystemServer:
private void startBootstrapServices() {
...
traceBeginAndSlog("StartActivityManager");
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
traceEnd();
...
// Set up the Application instance for the system process and get started.
traceBeginAndSlog("SetSystemProcess");
mActivityManagerService.setSystemProcess();
traceEnd();
...
}
这里是通过SystemServiceManager(一看名字就是SystemServer的管理类API-19新加的,老的源码是通过各个service的main方法去构造自己,比如ActivityManagerService.main())的startService()通过反射去构造各个服务.
AMS启动后还要通过setSystemProcess()去向ServiceManager注册自己
ActivityManagerService:
public void setSystemProcess() {
try {
ServiceManager.addService(Context.ACTIVITY_SERVICE, this, true);
ServiceManager.addService(ProcessStats.SERVICE_NAME, mProcessStats);
ServiceManager.addService("meminfo", new MemBinder(this));
ServiceManager.addService("gfxinfo", new GraphicsBinder(this));
ServiceManager.addService("dbinfo", new DbBinder(this));
if (MONITOR_CPU_USAGE) {
ServiceManager.addService("cpuinfo", new CpuBinder(this));
}
ServiceManager.addService("permission", new PermissionController(this));
ServiceManager.addService("processinfo", new ProcessInfoService(this));
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(
"android", STOCK_PM_FLAGS | MATCH_SYSTEM_ONLY);
mSystemThread.installSystemApplicationInfo(info, getClass().getClassLoader());
synchronized (this) {
ProcessRecord app = newProcessRecordLocked(info, info.processName, false, 0);
app.persistent = true;
app.pid = MY_PID;
app.maxAdj = ProcessList.SYSTEM_ADJ;
app.makeActive(mSystemThread.getApplicationThread(), mProcessStats);
synchronized (mPidsSelfLocked) {
mPidsSelfLocked.put(app.pid, app);
}
updateLruProcessLocked(app, false, null);
updateOomAdjLocked();
}
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(
"Unable to find android system package", e);
}
}
从代码上可以看出他不仅注册的自己的主业务(ACTIVITY_SERVICE),还有很多比如内存管理("meninfo"),cpu管理("cpuinfo"),权限管理("permission")等等诸多副业.
ActivityManagerService启动好之后,我们就可以去启动我们的应用程序了.Launcher程序就是一个特殊的应用程序.
Activity的启动也是个复杂的过程就放到下一节来讲吧.
类的总结:
ServiceManager:Binder机制中的"DNS服务器",负责域名(某Binder服务在ServiceManager注册时提供的名称)到IP地址(Binder驱动分配)的解析.
Zygote进程:字面意思"受精卵",是Android大多数应用和系统进程的孵化器.
SystemServer:大部分服务的宿主Service,SystemServer启动后就进入Launcher主界面了.
ActivityManagerService:寄生在SystemServer里.管理Activity,Service,Broadcast等生命周期.同时负责内存管理,CPU管理,权限管理等功能
源码版本:API-27