Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 插件化:apkplugdemo 源碼閱讀指南(10)

編輯:關於 Android 程式設計

閱讀本節前,建議先了解 apkplug 基礎教程。本教學基於 apkplug V1.6.8 版本編寫,最新開發方式請以官網為準。可下載最新的 apkplugdemo 源碼。

apkplugdemo 演示圖:

一、apkplugdemo 工程源碼結構

src 目錄下:

- com.apkplugdemo.adapter —— 插件列表 Adapter

- com.apkplugdemo.adapter.base —— Adapter 基類

- com.apkplugdemo.FileUtil —— 檔案操作類

- com.apkplugdemo.FileUtil.filter —— 檔案類型過濾類

- com.apkplugdemo.util —— 專案通用工具類

- com.apkplugdemo.util.Observer —— Java 觀察者設計模式類

- com.apkplugdemo.util.preferencesFactory —— preferences 操作類

- com.example.apkplugdemo —— 專案 Application 與 Activity 類

- huahua.viewpager —— 與 com.example.apkplugdemo 功能相同,只是以 fragment 方式展示

二、閱讀方式

由上述結構可知,除去工具類,真正需要閱讀的核心程式碼並不多:

- com.apkplugdemo.adapter.ListBundleAdapter —— 負責首頁列表 Item 展示及「運行」按鈕事件

- com.example.apkplugdemo.ProxyApplication —— 負責啟動 apkplug 框架,並安裝 assets 目錄下的插件(透過 InstallBundle 類安裝)

- com.example.apkplugdemo.MyProperty —— 啟動框架所需的介面類,為框架提供本地化變數保存與取得的介面(舊版本還提供自啟插件的安裝,v1.6.8 改由 BundleControl 服務替代)

- com.example.apkplugdemo.MainActivity —— 專案啟動類,展示已安裝插件列表,提供安裝 SD 卡中插件的按鈕等功能

- com.example.apkplugdemo.InstallBundle —— 啟動 assets 目錄下的插件

三、ProxyApplication

ProxyApplication 只負責啟動框架,然後呼叫 InstallBundle 啟動插件:

public void onCreate() {
    super.onCreate();
    try {
        List<BundleActivator> activators = new java.util.ArrayList<BundleActivator>();
        // 將服務加入框架,框架將在啟動時啟動這些服務
        activators.add(new appServiceManager());
        frame = FrameworkFactory.getInstance().start(activators, this,
                new MyProperty(this.getApplicationContext()));
        BundleContext context = frame.getSystemBundleContext();
        // 安裝 assets 資料夾下的插件(1.6.7 以上建議使用此新方式)
        InstallBundle ib = new InstallBundle();
        ib.installBundle(getApplicationContext(), context, new installCallback() {
            @Override
            public void callback(int arg0, Bundle arg1) {
                if (arg0 == installCallback.stutas5 || arg0 == installCallback.stutas7) {
                    Log.d("", String.format("插件安裝 %s : %d", arg1.getName(), arg0));
                    return;
                } else {
                    Log.d("", "插件安裝失敗 :%s" + arg1.getName());
                }
            }
        });
    } catch (Exception ex) {
        System.err.println("Could not create : " + ex);
        ex.printStackTrace();
        int nPid = android.os.Process.myPid();
        android.os.Process.killProcess(nPid);
    }
}

四、InstallBundle 安裝插件實作

InstallBundle 是呼叫 BundleControl 將 assets 目錄中的 apk 檔案安裝到宿主應用中:

// 從 assets 目錄複製 apk 檔案
InputStream in = context.getAssets().open("BundleDemoOSGIService1.apk");
File f0 = new File(context.getFilesDir(), "BundleDemoOSGIService1.apk");
if (!f0.exists()) {
    copy(in, f0);
    // 第一次啟動時執行安裝,以後就不再執行
    // startlevel 設為 1 插件會自啟;isCheckVersion 不檢測插件版本覆蓋更新
    this.install(mBundleContext, "file:" + f0.getAbsolutePath(), callback, 1, false);
}

五、MainActivity 介面程式碼

MainActivity 的初始化函式:

- initBundleList() —— 獲取已安裝插件

- ListenerBundleEvent() —— 監聽插件安裝事件

以上便是 apkplugdemo 中對 apkplug 框架的相關調用,其餘工具性的程式碼有興趣的讀者可自行研究。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved