Android 啟動(dòng)流程_第1頁(yè)
Android 啟動(dòng)流程_第2頁(yè)
Android 啟動(dòng)流程_第3頁(yè)
Android 啟動(dòng)流程_第4頁(yè)
Android 啟動(dòng)流程_第5頁(yè)
已閱讀5頁(yè),還剩11頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Android 啟動(dòng)流程 博客分類:Android資訊AndroidJavaITeye腳本Socket SystemServer的啟動(dòng)frameworks/base/services/java/com/android/server/SystemServer.java: run()其中調(diào)用ActivityManagerService.installSystemProviders(),最終會(huì)調(diào)用到frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/下的各個(gè)content providers的onC

2、reate()函數(shù),初始化數(shù)據(jù)庫(kù)內(nèi)容。android 啟動(dòng)流程other ref:Android從Linux系統(tǒng)啟動(dòng)有4個(gè)步驟;(1) init進(jìn)程啟動(dòng)(2) Native服務(wù)啟動(dòng)(3) System Server,Android服務(wù)啟動(dòng)(4) Home啟動(dòng)總體啟動(dòng)框架圖如:android 啟動(dòng)流程第一步:initial進(jìn)程(systemcoreinit) init進(jìn)程,它是一個(gè)由內(nèi)核啟動(dòng)的用戶級(jí)進(jìn)程。內(nèi)核自行啟動(dòng)(已經(jīng)被載入內(nèi)存,開始運(yùn)行,并已初始化所有的設(shè)備驅(qū)動(dòng)程序和數(shù)據(jù)結(jié)構(gòu)等)之后,就通過(guò)啟動(dòng)一個(gè)用戶級(jí)程序init的方式,完成引導(dǎo)進(jìn)程。init始終是第一個(gè)進(jìn)程.Init.rcInit.m

3、arvell.rcandroid 啟動(dòng)流程Init進(jìn)程一起來(lái)就根據(jù)init.rc和init.xxx.rc腳本文件建立了幾個(gè)基本的服務(wù): * servicemanamger * zygote。最后Init并不退出,而是擔(dān)當(dāng)起property service的功能。1.1腳本文件initSystem/Core/InitInit.c: parse_config_file(Init.rc) parse_config_file(Init.marvel.rc)解析腳本文件:Init.rc和Init.xxxx.rc(硬件平臺(tái)相關(guān))Init.rc是Android自己規(guī)定的初始化腳本(Android Init

4、Language, System/Core/Init/readme.txt)該腳本包含四個(gè)類型的聲明: * Actions * Commands * Services * Options.1.2 服務(wù)啟動(dòng)機(jī)制我們來(lái)看看Init是這樣解析.rc文件開啟服務(wù)的。(1)打開.rc文件,解析文件內(nèi)容 systemcoreinitinit.c將service信息放置到service_list中。 systemcoreinit parser.c(2)restart_service() systemcoreinitinit.cservice_startexecve().建立service進(jìn)程。第二步 Zyg

5、oteServicemanager和zygote進(jìn)程就奠定了Android的基礎(chǔ)。Zygote這個(gè)進(jìn)程起來(lái)才會(huì)建立起真正的Android運(yùn)行空間,初始化建立的Service都是Navtive service.在.rc腳本文件中zygote的描述:service zygote /system/bin/app_process -Xzygote /system/bin -zygote -start-system-server所以Zygote從main()frameworksbasecmdsapp_processapp_main.cpp開始。(1) main()frameworksbasecmdsap

6、p_processapp_main.cpp * 建立Java Runtime * runtime.start("ernal.os.ZygoteInit", startSystemServer);(2) runtime.startframeworks/base/core/jni/AndroidRuntime.cpp * 建立虛擬機(jī):startVM(.) * 運(yùn)行:ernal.os.ZygoteInit:main函數(shù)。(3)main()ernal.os.ZygoteInit/正真的Zygo

7、te。 * registerZygoteSocket();/登記Listen端口 * startSystemServer(); * 進(jìn)入Zygote服務(wù)框架。經(jīng)過(guò)這幾個(gè)步驟,Zygote就建立好了,利用Socket通訊,接收ActivityManangerService的請(qǐng)求,F(xiàn)ork應(yīng)用程序。第三步 System Server上fork了一個(gè)進(jìn)程: com.android.server.SystemServer.于是 SystemServer(SystemServer.java)就建立了。Android的所有服務(wù)循環(huán)框架都是建立 SystemServer(SystemServer.java)

8、上。在SystemServer.java中看不到循環(huán)結(jié)構(gòu),只是可以看到建立了 init2的實(shí)現(xiàn)函數(shù),建立了一大堆服務(wù),并AddService到service Manager。main() com/android/server/SystemServerinit1();Init1()是在Native空間實(shí)現(xiàn)的(com_andoird_server_systemServer.cpp)。我們一看這個(gè)函數(shù)就知道了,init1->system_init() System_init.cpp在system_init()我們看到了循環(huán)閉合管理框架。Call "com/android/server

9、/SystemServer", "init2".ProcessState:self()->startThreadPool(); IPCThreadState:self()->joinThreadPool();init2()SystemServer.java中建立了Android中所有要用到的服務(wù)。這個(gè)init2()建立了一個(gè)線程,來(lái)New Service和AddService來(lái)建立服務(wù)第三步 Home啟動(dòng)后半段,我們可以看到系統(tǒng)在啟動(dòng)完所有的Android服務(wù)后,做了這樣一些動(dòng)作:(1) 使用xxx.systemReady()通知各個(gè)服務(wù),系統(tǒng)已經(jīng)就緒

10、。(2) 特別對(duì)于ActivityManagerService.systemReady(回調(diào))Widget.wallpaper,imm(輸入法)等ready通知。Home就是在ActivityManagerService.systemReady()通知的過(guò)程中建立的。下面是ActivityManagerService.systemReady()的偽代碼:systemReady()ActivityManagerService.javaresumeTopActivityLocked()startHomeActivityLocked();/如果是第一個(gè)則啟動(dòng)HomeActivity。startAct

11、ivityLocked(。)CATEGORY_HOME android 啟動(dòng)流程In this topic you will learn some information about Android process management. First lets take a look at the launched processes during Android booting.USER PID PPID VSIZE RSS WCHAN PC NAMEroot 1 0 264 176 c00acc6c 0000c36c S /initroot 28 1 724 308 c0051354 af

12、e0c4cc S /system/bin/shsystem 30 1 796 248 c026516c afe0b74c S /system/bin/servicemanagerroot 31 1 1824 316 ffffffff afe0b50c S /system/bin/mountdroot 32 1 652 248 c02976e0 afe0c0bc S /system/bin/debuggerdradio 33 1 5344 664 ffffffff afe0bdbc S /system/bin/rildroot 34 1 71028 18828 c00ad308 afe0b874

13、 S zygotemedia 37 1 16812 3456 ffffffff afe0b74c S /system/bin/mediaserverroot 39 1 788 288 c02f9ae4 afe0b50c S /system/bin/installdsystem 86 34 187756 21836 ffffffff afe0b74c S system_serverradio 118 34 103476 13896 ffffffff afe0c824 S com.android.phoneapp_4 124 34 117848 19248 ffffffff afe0c824 S

14、cess.acoreapp_5 139 34 98672 11516 ffffffff afe0c824 S com.android.mmsapp_3 151 34 92096 10976 ffffffff afe0c824 S com.android.alarmclockapp_6 161 34 94436 12616 ffffffff afe0c824 S com.android.calendarapp_9 173 34 93248 11728 ffffffff afe0c824 S cess.mediaapp_15 182 34 91848 9

15、764 ffffffff afe0c824 S com.android.voicedialerapp_16 190 34 94524 10812 ffffffff afe0c824 S cess.imThey can be divided into three kinds.Root Processinit is the first process after kernel booting. The major task it performs:l Parser and execute init.rc and init.%hardware%.rcl Automaticall

16、y generate device node under /devl Start log and property servicel Monitor for device, property set and child process exit eventsNative Application ProcessAccording to init.rc, init will fork the following native application process.console: star a shell.servicemanager: start binder IPC service mana

17、ger.mountd: mount all fs defined in /system/etc/mountd.conf if started, receive commands through local socket to mount any fs.debuggerd: start debug system.rild: start radio interface layer daemon.zygote: start Android Java VM Runtime and start system server. Its the most important process.mediaserv

18、er: start AudioFlinger, MediaPlayerService and CameraService.installd: start install package daemon.JAVA Application ProcessEvery JAVA application process is forked from zygote process. system_server is a special JAVA process, which is directly forked from zygote. Other JAVA process is created from

19、ActivityManagerService(run in system_server process) like pid = Process.start("android.app.ActivityThread",mSimpleProcessManagement ? cessName : null, uid, uid,gids, (.flags&ApplicationInfo.FLAG_DEBUGGABLE) != 0), null);While Process.java use UNIX domain socket

20、to communicate with zygote. So the overall picture is shown as following.android 啟動(dòng)流程System ServerIts the first JAVA application launched by zygote. It starts the core Android services, e.g. ActivityManager, WindowManager, PackageManager etc. Its the Android core engine.Persistent ApplicationDuring

21、booting, ActivityManagerService.systemReady will start all persistent applications.List apps = ActivityThread.getPackageManager().getPersistentApplications(PackageManager.GET_SHARED_LIBRARY_FILES);if (apps != null) int N = apps.size();int i;for (i=0; i<N; i+) ApplicationInfo info= (ApplicationInf

22、o)apps.get(i);if (info != null &&!info.packageName.equals("android") addAppLocked(info);Currently only Phone application is registered as a persistent app in AndroidManifest.xml like this.<application android:name="PhoneApp"android:persistent="true"android:la

23、bel="string/dialerIconLabel"android:icon="drawable/ic_launcher_phone">So during booting, only phone application is automatically launched. Its the “com.android.phone” process.The First ActivityThe first activity is launched by senting Intent.CATEGORY_HOME intent from ActivityM

24、anagerService.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.getPackageMa

25、nager(),PackageManager.GET_SHARED_LIBRARY_FILES);if (aInfo != null) intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aI);/ Don't do this if the home app is currently being/ instrumented.ProcessRecord app = getProcessRecordLocked(aIcessName,aInfo.application

26、Info.uid);if (app = null | app.instrumentationClass = null) intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);startActivityLocked(null, intent, null, null, 0, aInfo,null, null, 0, 0, 0, false);Its the “cess.acore” process. (The process name is defined in AndroidManifest.x

27、ml)Auto-launched Application After BootingWhen activity idle is detected in ActivityManagerService, it will broadcast ACTION_BOOT_COMPLETED intent at the first time.if (mFactoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL) / Tell anyone interested that we are done booting!synchronized (this) broadcas

28、tIntentLocked(null, null,new Intent(Intent.ACTION_BOOT_COMPLETED, null),null, null, 0, null, null,android.Manifest.permission.RECEIVE_BOOT_COMPLETED,false, false, MY_PID, Process.SYSTEM_UID);Currently, MMS, AlarmClock, Calendar, MediaProvider, VoiceDialer and IM have registered as a receiver for ACT

29、ION_BOOT_COMPLETED intent in their AndroidManifest.xml. So they will be automatically launched. (This explains the remained JAVA process.)Email also registers as a receiver for ACTION_BOOT_COMPLETED intent in its AndroidManifest.xml, but it defines android:enable=”false”. So it wont be launched.<

30、receiver android:name=".service.BootReceiver"android:enabled="false"><intent-filter><action android:name="ent.action.BOOT_COMPLETED" /></intent-filter><intent-filter><action android:name="ent.action.DEVICE_STORAGE

31、_LOW" /></intent-filter><intent-filter><action android:name="ent.action.DEVICE_STORAGE_OK" /></intent-filter></receiver>DownloadProvider also registers as a receiver for ACTION_BOOT_COMPLETED intent in its AndroidManifest.xml, but it defines

32、 android:exported=”false”. So it wont be launched.<receiver android:name=".DownloadReceiver" android:exported="false"><intent-filter><action android:name="ent.action.BOOT_COMPLETED" /><action android:name=".conn.CONNECTIVITY_CHANGE&q

33、uot; /></intent-filter></receiver>Behind the JAVA processsystem_server is a special case. It calls ActivityThread.javas systemMain static function, which creates an instance of ActivityThread. ActivityThread then creates an instance of ApplicationThread, Application and ApplicationCon

34、text.Every other JAVA process works in a different way. Its controlled by system_server while forked by zygote. When any JAVA process other than system_server is forked from zygote, it automatically calls ActivityThread.javas main static function(See Process.java and the following code snippet).try ZygoteInit.invokeStaticMain(cloader, className, mainArgs); catc

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論