編輯:關於Android編程
首先看一張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();
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方法。

仿餓了嗎點餐界面ListView聯動的實現
在上篇文章給大家介紹了仿餓了嗎點餐界面兩個ListView聯動效果主要實現了2個ListView怎樣實現互相關聯,正好上篇博客review了ListView控件常規使用,
Android之Intent使用
intent簡介 Intent對象的組成 顯式Intent與隱式Intent 顯式Intent 隱式Intentintent簡介intent用於組件之間的跳轉,它在程序運
Android中RecyclerView布局代替GridView實現類似支付寶的界面
單純使用GridView通用的兩種給GridView 添加分割線的方法;http://stackoverflow.com/questions/7132030/androi
Android惡意代碼編年史
概述 移動互聯網安全無疑已成為當今主流安全威脅之一。統計 2011 年至今的移動惡意代碼病毒庫,可以看到 Android 平台下的惡意軟件數量增長可謂