android运行过程简书,Android系统的启动流程
Android系統(tǒng)有哪些進(jìn)程
在Linux系統(tǒng)啟動(dòng)時(shí),會(huì)讀取init.rc,里面配置了一些需要啟動(dòng)的進(jìn)程。注意:SystemServer進(jìn)程不在init.rc里,因?yàn)镾ystemServer進(jìn)程是由zygote啟動(dòng)的。
如下所示:
service zygote /system/bin/app_process ...
service servicemanager /system/bin/servicemanager ...
service sufaceflinger /system/bin/suerfaceflinger ...
service media /system/bin/mediaserver ...
...
Zygote是怎么啟動(dòng)的?
init進(jìn)程fork出zygote進(jìn)程
啟動(dòng)虛擬機(jī),注冊(cè)JNI函數(shù)
預(yù)加載系統(tǒng)資源(常用類、主題資源、JNI函數(shù)、共享庫等)
啟動(dòng)SystemServer
private static boolean startSystemServer(...){
String args[] = {
...
"com.android.server.SystemServer",
};
int pid = Zygote.forkSystemServer(...);
if(pid==0){
handleSystemServerProcess(parsedArgs);
}
return true;
}
......
void handleSystemServerProcess(Arguments parsedArgs){
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion,parsedArgs.remainingArgs,...);
}
......
void zygoteInit(String[] argv,...){
commonInit(); //常規(guī)初始化
nativeZygoteInit(); //調(diào)用native的onZygoteInit,啟動(dòng)binder。
applicationInit(argv,...); //調(diào)用Java類(SystemServer)的main函數(shù)
}
......
virtual void onZygoteInit(){
sp proc = ProcessState::self();
proc->startThreadPool();
}
......
void applicationInit(...){
invokeStaticMain(args,...);//調(diào)用Java類(SystemServer)的main函數(shù)
}
......
SystemServer.java
public static void main(String[] args){
new SystemServer().run();
}
......
private void run(){
Looper.prepareMainLooper(); //創(chuàng)建主線程looper
System.loadLibrary("android_servers"); //加載native層的SystemServer代碼
createSystemContext(); //創(chuàng)建系統(tǒng)上下文
startBootstrapServices(); //啟動(dòng)引導(dǎo)服務(wù)
startCoreServices(); //啟動(dòng)核心服務(wù)
startOtherServices(); //啟動(dòng)其他服務(wù)
Looper.loop(); //開啟循環(huán)
}
進(jìn)入Socket Loop
boolean runOnce(){
String[] args = readArgumentList();
int pid = Zygote.forkAndSpecialize();
if(pid==0){
//in child process
handleChildProc(args,...);
//should never get here, the child is expected to either throw ZygoteInit.MethodAndArgsCaller or exec().
return true;
}else{
return handleParentProc(pid,...);
}
}
系統(tǒng)服務(wù)是怎么發(fā)布,讓應(yīng)用程序可見?
void publisBinderService(String name,IBinder service){
publishBinderService(name,service,false);
}
void publishBinderService(String name,IBinder service,...){
ServiceManager.addService(name,service,allowIsolated);
}
系統(tǒng)服務(wù)運(yùn)行在什么線程?
工作線程:AMS、PMS、PackageManagerService
還有一些運(yùn)行在公用的線程中,如在DisplayThread,FgThread,IoThread,UiThread線程中運(yùn)行。
binder線程:應(yīng)用接收到binder調(diào)用后,啟動(dòng)線程。
系統(tǒng)服務(wù)的互相依賴是如何解決的?
分批啟動(dòng)
AMS, PMS, PKMS
分階段啟動(dòng)
階段1,階段2,階段3,……
桌面的啟動(dòng)
public void systemReady(final Runnable goingCallback){
……
//啟動(dòng)桌面程序的Launcher類
startHomeActivityLocked(mCurrentUserId,"systemReady");
……
}
//Launcher(Activity)的onCreate方法中會(huì)創(chuàng)建一個(gè)LoaderTask
mLoaderTask = new LoaderTask(mApp.getContext(),loadFlags);
//在LoaderTask中會(huì)去向Package ManagerSevice查詢已安裝應(yīng)用,然后將應(yīng)用圖標(biāo)顯示在桌面,當(dāng)用戶點(diǎn)擊圖標(biāo)時(shí),Launcher就會(huì)啟動(dòng)應(yīng)用程序。
mPm.queryIntentActivitiesAsUser();
總結(jié)
以上是生活随笔為你收集整理的android运行过程简书,Android系统的启动流程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个女汉子的个性签名。
- 下一篇: android如何看百分比版本,【JAV