android运行过程简书,Android系统的启动流程
Android系統有哪些進程
在Linux系統啟動時,會讀取init.rc,里面配置了一些需要啟動的進程。注意:SystemServer進程不在init.rc里,因為SystemServer進程是由zygote啟動的。
如下所示:
service zygote /system/bin/app_process ...
service servicemanager /system/bin/servicemanager ...
service sufaceflinger /system/bin/suerfaceflinger ...
service media /system/bin/mediaserver ...
...
Zygote是怎么啟動的?
init進程fork出zygote進程
啟動虛擬機,注冊JNI函數
預加載系統資源(常用類、主題資源、JNI函數、共享庫等)
啟動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(); //常規初始化
nativeZygoteInit(); //調用native的onZygoteInit,啟動binder。
applicationInit(argv,...); //調用Java類(SystemServer)的main函數
}
......
virtual void onZygoteInit(){
sp proc = ProcessState::self();
proc->startThreadPool();
}
......
void applicationInit(...){
invokeStaticMain(args,...);//調用Java類(SystemServer)的main函數
}
......
SystemServer.java
public static void main(String[] args){
new SystemServer().run();
}
......
private void run(){
Looper.prepareMainLooper(); //創建主線程looper
System.loadLibrary("android_servers"); //加載native層的SystemServer代碼
createSystemContext(); //創建系統上下文
startBootstrapServices(); //啟動引導服務
startCoreServices(); //啟動核心服務
startOtherServices(); //啟動其他服務
Looper.loop(); //開啟循環
}
進入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,...);
}
}
系統服務是怎么發布,讓應用程序可見?
void publisBinderService(String name,IBinder service){
publishBinderService(name,service,false);
}
void publishBinderService(String name,IBinder service,...){
ServiceManager.addService(name,service,allowIsolated);
}
系統服務運行在什么線程?
工作線程:AMS、PMS、PackageManagerService
還有一些運行在公用的線程中,如在DisplayThread,FgThread,IoThread,UiThread線程中運行。
binder線程:應用接收到binder調用后,啟動線程。
系統服務的互相依賴是如何解決的?
分批啟動
AMS, PMS, PKMS
分階段啟動
階段1,階段2,階段3,……
桌面的啟動
public void systemReady(final Runnable goingCallback){
……
//啟動桌面程序的Launcher類
startHomeActivityLocked(mCurrentUserId,"systemReady");
……
}
//Launcher(Activity)的onCreate方法中會創建一個LoaderTask
mLoaderTask = new LoaderTask(mApp.getContext(),loadFlags);
//在LoaderTask中會去向Package ManagerSevice查詢已安裝應用,然后將應用圖標顯示在桌面,當用戶點擊圖標時,Launcher就會啟動應用程序。
mPm.queryIntentActivitiesAsUser();
總結
以上是生活随笔為你收集整理的android运行过程简书,Android系统的启动流程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个女汉子的个性签名。
- 下一篇: android如何看百分比版本,【JAV