标签:
首先看一张Android系统启动流程图:
一个进程最重要的两项指标一个是启动了Binder线程池,也就是可以进程Binder进程间通信了。另一个是启动了Handler消息循环,可以使用了消息循环机制。
1、那么systemserver进程是什么时候实现上面两个机制的呢?见代码:
启动了Binder线程池。是子线程池。
public static final void zygoteInit(String[] argv)
throws ZygoteInit.MethodAndArgsCaller
{
......
zygoteInitNative();
......
} 启动了Hander消息循环机制,是子线程的Handler:public static final void init2() {
Slog.i(TAG, "Entered the Android system server!");
Thread thr = new ServerThread();
thr.setName("android.server.ServerThread");
thr.start();
}class ServerThread extends Thread {
@Override
public void run() {
EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN,
SystemClock.uptimeMillis());
Looper.prepare();
.....
try {
......
Slog.i(TAG, "Activity Manager");
context = ActivityManagerService.main(factoryTest);//初始化了ActivityTask
......
Slog.i(TAG, "Package Manager");
pm = PackageManagerService.main(context,
factoryTest != SystemServer.FACTORY_TEST_OFF);//初始化了PackageMangerService
ActivityManagerService.setSystemProcess();
.....
wm = WindowManagerService.main(context, power,
factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
ServiceManager.addService(Context.WINDOW_SERVICE, wm);
......
((ActivityManagerService)ActivityManagerNative.getDefault())
.systemReady(new Runnable() {
public void run() {
}
});
Looper.loop();
Slog.d(TAG, "System ServerThread is exiting!");
}
} 2、那么应用程序进程是什么时候实现上面两个机制的呢?见代码:
启动了Binder线程池,和上面一样,都是子线程池。
public static final void zygoteInit(String[] argv)
throws ZygoteInit.MethodAndArgsCaller
{
......
zygoteInitNative();
......
} 启动了Hander消息循环机制,是主线程的Handler:public static final void main(String[] args) {
SamplingProfilerIntegration.start();
Process.setArgV0("<pre-initialized>");
Looper.prepareMainLooper();
if (sMainThreadHandler == null) {
sMainThreadHandler = new Handler();
}
ActivityThread thread = new ActivityThread();
thread.attach(false);
....
Looper.loop();
......
}((ActivityManagerService)ActivityManagerNative.getDefault())
.systemReady(new Runnable() {
public void run() {
}
}); 参考Android系统源代码情景分析一书,最后systemReady最终执行到了这块:boolean startHomeActivityLocked() {
if (mFactoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null) {
// We are running in factory test mode, but unable to find
// the factory test app, so just sit around displaying the
// error message and don‘t try to start anything.
return false;
}
Intent intent = new Intent(
mTopAction,
mTopData != null ? Uri.parse(mTopData) : null);
intent.setComponent(mTopComponent);
if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) {
intent.addCategory(Intent.CATEGORY_HOME);
}
ActivityInfo aInfo =
intent.resolveActivityInfo(mContext.getPackageManager(),
STOCK_PM_FLAGS);
if (aInfo != null) {
intent.setComponent(new ComponentName(
aInfo.applicationInfo.packageName, aInfo.name));
// Don‘t do this if the home app is currently being
// instrumented.
ProcessRecord app = getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid);
if (app == null || app.instrumentationClass == null) {
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mMainStack.startActivityLocked(null, intent, null, null, 0, aInfo,
null, null, 0, 0, 0, false, false);
}
}
return true;
} 这是在SystemServer的子线程中执行的,mMainStack.startActivityLocked的执行代码,请参考Android系统源代码情景分析一书,执行不同出现在resumeTopActivityLocked这个方法中,由于mResumedActivity等于NULL,所以此时执行,startSpecificActivityLocked(next, true, true)。最后执行到startProcessLocked,里面这句话是关键。int pid = Process.start("android.app.ActivityThread",
mSimpleProcessManagement ? app.processName : null, uid, uid,
gids, debugFlags, null);Message msg = mHandler.obtainMessage(PROC_START_TIMEOUT_MSG);
msg.obj = app;
mHandler.sendMessageDelayed(msg, PROC_START_TIMEOUT); 其中mHander为final Handler mHandler = new Handler() {},这个Handler是ServerThread子线程中的handler。发送消息时,只调用了Looper.prepare();返回后才调用Looper.loop();Process.start创建了一个应用程序进程,开启了Binder线程池和Handler消息循环机制,请参考第2点。
1、thread.attach(false)是Launcher主线程与SystemServer子线程Binder进程间通信,发送ATTACH_APPLICATION_TRANCTION;SystemServer子线程(Binder线程池)移除SystemServer中的ServerThread子线程(Handler消息循环)里面的Handler里面的消息。
2、SystemServer子线程向Laumcher子线程发送SCHEDULE_LAUNCH_ACTIVITY_TARNSACTION;之后SystemServer子线程继续循环等待,并向Launcher子线程发送返回数据。
3、Laucher主线程返回后调用了Looper.loop(),主线程消息循环机制正式创立。
4、处理刚才由SystemServer子线程发送Lanncher子线程的数据(单向),Launcher子线程最后通过消息处理机制,在主线程调用了Launcher的onCreate方法。
标签:
原文地址:http://blog.csdn.net/jltxgcy/article/details/46669147