Android FrameWork——PackageManager框架

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中.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值