BundleManager

public final class BundleManager extends Object

├── com.iappease.mozart
    └── BundleManager

BundleManager类主要作用为框架初始化,插件管理包括插件的生命周期管理等,并且所有的异步方法都是通过广播回调用户注册的BroadcastReceiver通知给开发者(此广播仅在应用内部发布和接收,外部应用无法接收和监听),需先调用BundleManager.getInstance()获取BundleManager实例。

概览

Public Method
void downloadBundle(BundleLite lite)
下载Bundle插件
void init(Context context, int appkey, String channel)
初始化BundleManager,参数不能为null
void fetchUpdateBundleInfo()
获取云端Bundle信息
static BundleManager getInstance()
获取BundleManager实例
void launch(Context context)
启动BundleManager
void onDestroy()
销毁BundleManager
void registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
注册Receiver
void removeBundle(BundleLite lite)
删除Bundle
void startBundle(Context context, BundleLite lite)
启动Bundle
void unregisterReceiver(BroadcastReceiver receiver)
注销Receiver
void addShortcut(BundleLite lite, Class class)
添加桌面图标

downloadBundle

void downloadBundle(BundleLite lite)

根据传入的BundleLite参数,下载Bundle插件;该方法支持加载本地插件,需配置BundleLite,参见loadLocalBundle()代码示例

参数 描述
BundleLite 包含Bundle插件相关信息的类
回调广播中的Intent对应的Action Action对应描述
Constant.ACTION_BUNDLE_DOWNLOAD_START Bundle开始下载
Constant.ACTION_BUNDLE_DOWNLOAD_ERROR Bundle下载异常
Constant.ACTION_BUNDLE_DOWNLOAD_ING Bundle正在下载中
Constant.ACTION_BUNDLE_DOWNLOAD_COMPLETE Bundle下载完成
Constant.ACTION_BUNDLE_EXIST Bundle已经存在,无需下载
回调广播中的Intent数据对应的Key 数据类型
Constant.BUNDLE_KEY_VALUE BundleLite

代码示例片段:


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == Constant.ACTION_BUNDLE_DOWNLOAD_COMPLETE) {
                BundleLite lite = (BundleLite) intent.getSerializableExtra(Constant.BUNDLE_KEY_VALUE);
                //other operate
            } else if (action == Constant.ACTION_BUNDLE_DOWNLOAD_ERROR) {
                //other operate
            } else if (action == Constant.ACTION_BUNDLE_DOWNLOAD_START) {
                //other operate
            } else if (action == Constant.ACTION_BUNDLE_DOWNLOAD_ING) {
                //other operate
            } else if (action == Constant.ACTION_BUNDLE_EXIST) {
                //other operate
            }
        }
};
 
BundleManager.getInstance().registerReceiver(receiver, intetFilter);
BundleManager.getInstance().downloadBundle(bundle);
 

      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /**
   * 初始化本地assets目录中的Bundle信息
   * com.tpcstld.twozerogame_1.1.7_40.bundle
   */
  private void loadLocalBundle() {
    //记载本地游戏bundle
    File bundle = new File(cacheDir.getAbsolutePath() + File.separator + LOCAL_BUNDLE_DEMO);
    if (bundle.exists()) {
      lite = new BundleLite();
      lite.pkgVersionCode = "40";
      lite.pkgVersionName = "1.1.7";
      lite.pkgUrl = "file:/" + bundle.getAbsolutePath();
      lite.pkgName = "com.tpcstld.twozerogame";
      BundleManager.getInstance().downloadBundle(lite);
    }
  }
 

init

void init(Context context, int appkey, String channel)

初始化BundleManager, 在应用的入口Activity或者Application中调用

参数 描述
Context Context,框架默认使用Application Context
int 开发者申请的appkey,对应于云端的推送和插件的渠道发布维度
String 开发者定义的渠道号,对应于云端的推送和插件的渠道发布维度

代码示例片段:


      
1
2
BundleManager.getInstance().init(context, 123456, "appstore");
 

fetchUpdateBundleInfo

void fetchUpdateBundleInfo()

获取云端插件列表,包括插件的各个状态(已发布、已下线等)

回调广播中的Intent对应的Action Action对应描述
Constant.ACTION_BUNDLE_LIST_INFO Bundle信息获取成功
Constant.ACTION_BUNDLE_LIST_ERROR Bundle信息获取失败
回调广播中的Intent数据对应的Key 数据类型
Constant.BUNDLE_KEY_VALUES List<BundleLite>

代码示例片段:


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == Constant.ACTION_BUNDLE_LIST_INFO) {
                 List<BundleLite> list = (List<BundleLite>) intent.getSerializableExtra(Constant.BUNDLE_KEY_VALUES);
                //other operate
            } else if (action == Constant.ACTION_BUNDLE_LIST_ERROR) {
                //other operate
            }
        }
};
 
BundleManager.getInstance().registerReceiver(receiver, intetFilter);
BundleManager.getInstance().fetchUpdateBundleInfo();
 

launch(Context context)

void launch(Context context)

启动BundleManager, 必须之前已经调用过init()方法

参数 描述
Context Context,框架默认使用Application Context
回调广播中的Intent对应的Action Action对应描述
Constant.ACTION_BUNDLE_LOAD_SUCCESS 加载本地Bundle完成
Constant.ACTION_BUNDLE_IS_EMPTY 本地无可用Bundle
回调广播中的Intent数据对应的Key 数据类型
Constant.BUNDLE_KEY_VALUES List<BundleLite>

代码示例片段:


      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action == Constant.ACTION_BUNDLE_LOAD_SUCCESS) {
                 List<BundleLite> list = (List<BundleLite>) intent.getSerializableExtra(Constant.BUNDLE_KEY_VALUES);
                //other operate
            } else if (action == Constant.ACTION_BUNDLE_IS_EMPTY) {
                //other operate
            }
        }
};
 
BundleManager.getInstance().registerReceiver(receiver, intetFilter);
BundleManager.getInstance().launch();
 

onDestroy

void onDestroy()

销毁BundleManager, 注意:在应用的退出时调用

代码示例片段:


      
1
2
BundleManager.getInstance().onDestroy();
 

registerReceiver

void registerReceiver(BroadcastReceiver receiver, IntentFilter filter)

注册回调Receiver,注意:当一些回调使用完成后请及时注销Receiver

参数
BroadcastReceiver BroadcastReceiver,回调Receiver
IntentFilter 回调Receiver匹配的IntentFilter

代码示例片段:


      
1
2
BundleManager.getInstance().registerReceiver(receiver, intetFilter);
 

removeBundle

void removeBundle(BundleLite lite)

删除Bundle注意:当要删除的Bundle插件已经加载并正在运行时,会等到插件生命周期结束后才会删除

参数 描述
BundleLite 包含Bundle插件相关信息的类

代码示例片段:


      
1
2
BundleManager.getInstance().removeBundle(bundle);
 

startBundle

void startBundle(Context context, BundleLite lite)

启动Bundle,会默认启动Bundle插件中AndroidManifest.xml文件中声明的入口Activity


      
1
2
3
4
5
<intent-filter>
      <action android:name="android.intent.action.MAIN"/>
      <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
 
参数 描述
Context Context
BundleLite 包含Bundle插件相关信息的类

代码示例片段:


      
1
2
BundleManager.getInstance().removeBundle(bundle);
 

unregisterReceiver

void unregisterReceiver(BroadcastReceiver receiver)

注销回调Receiver

参数 描述
BroadcastReceiver BroadcastReceiver,回调Receiver

代码示例片段:


      
1
2
BundleManager.getInstance().unregisterReceiver(receiver, intetFilter);
 

addShortcut

void addShortcut(BundleLite lite, Class class)

根据传入的BundleLite参数,添加桌面图标

参数 描述
BundleLite 包含Bundle插件相关信息的类
Class 新建桌面图标的入口类,即启动页Activity

代码示例片段:


      
1
2
BundleManager.getInstance().addShortcut(lite, class);