1.接著前面講的ActivityManager架構,繼續說一下系統另一個重要的架構,PackagerManager
同樣先看一下靜態類結構圖:
大 部分情況我們是在Activity中使用getPackageManager方法擷取一個ApplicationPackageManager的對 象,ApplicationPackageManager實際上是封裝了一個IPackageManager.Stub.Proxy的對象
由IPackageManager.Stub.Proxy代理執行PackageManager相關操作,IPackageManager.Stub.Proxy實際代理的是PackageManagerService,
2.看了前面說的,可能你有點暈,我們再來重新理一下:
首先是IPackageManager是通過IPackageManager.aidl檔案產生,同時產生了存根類IPackageManager.Stub,代理類:IPackageManager.Stub.Proxy
這個是packageManager進程通訊的基本架構,我前面blog有說,不多加說明了
然後PackageManagerService,它繼承了IPackageManager.Stub,它作為PackageManager動作的實際執行者,在system_process中存在
再是我們使用者應用程式中的ApplicationPackageManager,先看它如何被擷取的:
ContextImpl.java中有一個方法:
public PackageManager getPackageManager() {
if (mPackageManager != null) {
return mPackageManager;
}
IPackageManager pm = ActivityThread.getPackageManager();
if (pm != null) {
// Doesn't matter if we make more than one instance.
return (mPackageManager = new ApplicationPackageManager(this, pm));
}
return null;
}
ApplicationPackageManager實際上是封裝了一個IPackageManager對象(IPackageManager.Stub.Proxy),當我們調用queryIntentActivities時,實際通過代理對象去執行:
public List<ResolveInfo> queryIntentActivities(Intent intent,
int flags) {
try {
return mPM.queryIntentActivities(//mPM是IPackageManager.Stub.Proxy對象
intent,
intent.resolveTypeIfNeeded(mContext.getContentResolver()),
flags);
} catch (RemoteException e) {
throw new RuntimeException("Package manager has died", e);
}
}
進過進程通訊,在PackageManagerService執行對應操作:
3.PackageManagerService的構建與擷取
--PackageManagerService 的構建:在system_process進程載入時,PackageManagerService被構建,在 SystemServer.ServerThread.run中有如下一段代碼,它就是載入 PackageManagerService的:
Slog.i(TAG, "Package Manager");
pm = PackageManagerService.main(context,
factoryTest != SystemServer.FACTORY_TEST_OFF);//啟動PackageManagerService
///////////////////////PackageManagerService///////////////////////////////////////////////////////////////////////////
public static final IPackageManager main(Context context, boolean factoryTest) {
PackageManagerService m = new PackageManagerService(context, factoryTest);
ServiceManager.addService("package", m);
return m;
}
--PackageManagerService擷取:
先看前面在ContextImpl.java->getPackagerManager中:
IPackageManager pm = ActivityThread.getPackageManager();
/////////////////////ActivityThread////////////////
public static IPackageManager getPackageManager() {
if (sPackageManager != null) {
//Slog.v("PackageManager", "returning cur default = " + sPackageManager);
return sPackageManager;
}
IBinder b = ServiceManager.getService("package");
//Slog.v("PackageManager", "default service binder = " + b);
sPackageManager = IPackageManager.Stub.asInterface(b);
//Slog.v("PackageManager", "default service = " + sPackageManager);
return sPackageManager;
}
從ServiceManager中擷取的服務pakager,該服務在.PackageManagerService的構建時被註冊到 ServiceManager中的,ServiceManager機制暫時沒有深入瞭解,後面再發blog專門說一下ServiceManager
原文:http://blog.csdn.net/stonecao/article/details/6591454