Android中ActivityManagerService的疑問與思考
ActivityThread代表應用程序的主執行緒,SystemServer並不是應用程序,為什麼SystemServer程序裡邊需要ActivityThread呢?
其實SystemServer也會包含一些Activity,比如系統關機對話方塊等,因此可以說,SystemServer是一個特殊的應用程序。應用程序指那些執行APK的程序,它們由Zygote 派生(fork)而來,上面運行了dalvik虛擬機器。與應用程序相對的就是系統程序(包括Zygote和SystemServer)。
注意,在應用程序中,通過呼叫 ActivityThread
類的 main()
函式得到 ActivityThread
的物件,而在SystemServer程序中,則是呼叫 systemMain()
函式。在SystemServer程序中, ActivityThread
物件是由一般的執行緒建立的,而在應用程序中, ActivityThread
物件是由應用主執行緒建立的。
Context是什麼?
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Context的類層次結構
ContextWrapper
是
ContextImpl
的代理類,在
ContextWrapper
中是
mBase
變數,
ContextWrapper
的內部實現最終都是由
ContextImpl
實現的,這樣做的目的是把
ContextImpl
隱藏起來。
framework-res.apk是什麼?
可以學習《深入理解PackageManagerService》,它的包名是“android”。思考framework-res.apk僅供SystemServer使用嗎?
PackageManagerService屬於SystemServer程序嗎?
待考察。
應用程序也載入了framework-res.apk這個包,所以應用程序裡邊有兩個Application?
待考察。
ActivityManagerService程序怎麼與應用程序互動?
ActivityManagerService
物件中的 ActivityThread
儲存著一個成員變數,叫做 ApplicationThread
,它實現了 IApplicationThread
介面,是個 Binder
物件,其中有一些成員方法,比如 scheduleStopActivity()
, scheduleLaunchActivity()
, scheduleStopService()
等,SystemServer程序通過 ActivityThread
物件的這些方法,傳送訊息給應用程序的 ActivityThread
中的 ApplicationThread
,呼叫相應的方法,從而在主執行緒中執行相應元件的生命週期方法。
多個Apk可以執行在同一個程序?
SystemServer已經載入了 framework-res.apk ,現在又要載入另外一個APK檔案,這就是多個APK執行在同一程序的典型案例。