Android FrameWork——PackageManager架構

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.