博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android FrameWork——PackageManager框架
阅读量:6766 次
发布时间:2019-06-26

本文共 2854 字,大约阅读时间需要 9 分钟。

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

转载于:https://www.cnblogs.com/xiaoxiaoboke/archive/2012/02/08/2342880.html

你可能感兴趣的文章
C# Lambda表达式
查看>>
Android Studio中多项目共享Library
查看>>
用java的io流,将一个文本框的内容反转
查看>>
python: 不同级别的日志输出到不同文件的日志类
查看>>
[LeetCode] Trapping Rain Water
查看>>
linux下下载redis,并且编译
查看>>
FirstOrDefault()的重载方法
查看>>
SumElemet
查看>>
团队编程
查看>>
ajax轮询session阻塞问题
查看>>
C++三大特性之封装
查看>>
【总结整理】WebGIS学习-thinkGIS(三):关于影像金字塔、瓦片行列号、分辨率resolution...
查看>>
冒泡排序的探究
查看>>
原生网络请求以及AFN网络请求/异步下载
查看>>
h3c 云计算管理平台
查看>>
C#读取网络流,读取网络上的js文件
查看>>
韩星5,6号 一锅双星技巧
查看>>
golang-MD5
查看>>
sof文件和NIOS II的软件(elf)合并为jic文件以使用Quartus Programmer烧写
查看>>
【前台 】字符串和js对象的相互转化
查看>>