Android培訓班(35) – Android移動開發技術文章_手機開發 Android移動開發教學課程

<!– @page { margin: 2cm } P { margin-bottom: 0.21cm } –>


在init.rc文件裡,可以看到加載下面的服務:


service zygote /system/bin/app_process -Xzygote /system/bin –zygote –start-system-server


socket zygote stream 666


onrestart write /sys/android_power/request_state wake


onrestart write /sys/power/state on


onrestart restart media


zygote服務的代碼在目錄:


Android-2.0/frameworks/base/cmds/app_process


 


zygote服務主要作用就是創建一個服務監聽用戶的命令,並且根據用戶的命令創建應用程序運行。


它的主要入口函數代碼如下:


int main(int argc, const char* const argv[])


{


// These are global variables in ProcessState.cpp


mArgC = argc;


mArgV = argv;


mArgLen = 0;


for (int i=0; i<argc; i++) {


mArgLen += strlen(argv[i]) + 1;


}


mArgLen–;


AppRuntime runtime;


這裡創建JAVA運行庫對象。


const char *arg;


const char *argv0;


argv0 = argv[0];


// Process command line arguments


// ignore argv[0]


argc–;


argv++;


// Everything up to — or first non – arg goes to the vm


int i = runtime.addVmArguments(argc, argv);


這裡添加命令行參數。


// Next arg is parent directory


if (i < argc) {


runtime.mParentDir = argv[i++];


}


// Next arg is startup classname or “–zygote”


if (i < argc) {


arg = argv[i++];


if (0 == strcmp(“–zygote”, arg)) {


bool startSystemServer = (i < argc) ?


strcmp(argv[i], “–start-system-server”) == 0 : false;


setArgv0(argv0, “zygote”);


set_process_name(“zygote”);


runtime.start(“com.android.internal.os.ZygoteInit”,


startSystemServer);


這裡根據命令行參數來創建JAVA虛擬機,並從 com.android.internal.os.ZygoteInit開始運行,然後啟動所有JAVA的服務。


} else {


set_process_name(argv0);


runtime.mClassName = arg;


// Remainder of args get passed to startup class main()


runtime.mArgC = argc-i;


runtime.mArgV = argv+i;


LOGV(“App process is starting with pid=%d, class=%s.
“,


getpid(), runtime.getClassName());


runtime.start();


}


} else {


LOG_ALWAYS_FATAL(“app_process: no class name or –zygote supplied.”);


fprintf(stderr, “Error: no class name or –zygote supplied.
“);


app_usage();


return 10;


}


}

發佈留言