外置插件,应该是这样使用吧:
在页面上显示一个插件图标或者按钮,第一次点击的时候,就开始下载,下载完了就打开。第二次点击的时候,就能自动打开了,并且开始检查插件版本,有新版本就下载下来,然后预加载,下一次打开就是最新的版本了。
效果图.gif
那在宿主的代码就是这样:
//打开外置插件
public void qifumingdeng(View view) {
final Button btn_qifu = (Button) findViewById(R.id.btn_qifu);
btn_qifu.setClickable(false);
MMCPlugin.getInstance().openPlugin(this, "demo4", "oms.mmc.GongdenggeActivity", new MMCPlugin.InstallListener() {
@Override
public void onInstalling(int progress) {
btn_qifu.setText("正在下载祈福明灯插件:" + progress + "%");
if (progress == 100) {
btn_qifu.setText("正在安装祈福明灯插件...");
}
}
@Override
public void onFail(String msg) {
btn_qifu.setClickable(true);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess() {
btn_qifu.setClickable(true);
btn_qifu.setText("打开祈福明灯插件");
}
});
}
看代码,有三个监听,第一个onInstalling,是下载插件进度,在这里更新UI进度条。第二个onFail,是下载或者安装插件失败,在这里做一个提示。第三个是onSuccess,无论是初次还是第二次打开,打开插件Activity都会回调到这里的。
那封装的类如下:
public class MMCPlugin {
private static MMCPlugin instance = null;
private MMCPlugin() {
}
public static MMCPlugin getInstance() {
if (instance == null) {
instance = new MMCPlugin();
}
return instance;
}
/**
* 打开插件
*
* @param context
* @param pluginName
* @param activityName
* @param installListener
*/
public void openPlugin(Context context, String pluginName, String activityName, InstallListener installListener) {
this.installListener = installListener;
if (RePlugin.isPluginInstalled(pluginName)) {//判断是否已经安装,安装了的话,就打开Activity,并且检查插件版本,需要更新的话就下载插件
RePlugin.startActivity(context, RePlugin.createIntent(pluginName, activityName));
if (installListener != null) {
installListener.onSuccess();
}
PluginInfo info = RePlugin.getPluginInfo(pluginName);
if (info.getVersion() < 2) {
//版本号由你们接口获得,然后进行对比,插件版本低于接口的版本就下载更新
downPlugin(context, "http://插件地址", pluginName, activityName, true);
}
} else {
downPlugin(context, "http://插件地址", pluginName, activityName, false);
}
}
/**
* 安装插件
*
* @param context
* @param pluginName
* @param activityName
*/
public void installPlugin(final Context context, final String pluginName, final String activityName, boolean isUpdate) {
final PluginInfo info = RePlugin.install(Environment.getExternalStorageDirectory() + "/" + pluginName + ".apk");
if (info != null) {
if (isUpdate) {//判断,是否为更新,如果是更新就预加载,下次打开就是最新的插件,不是更新就开始安装
RePlugin.preload(info);
} else {
new Thread(new Runnable() {
@Override
public void run() {
RePlugin.startActivity(context, RePlugin.createIntent(info.getName(), activityName));
((Activity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
if (installListener != null) {
installListener.onSuccess();
}
}
});
}
}).start();
}
} else {
if (installListener != null) {
installListener.onFail("安装失败");
}
}
}
/**
* 下载插件
*
* @param context
* @param fileUrl
* @param pluginName
* @param activityName
*/
public void downPlugin(final Context context, String fileUrl, final String pluginName, final String activityName, final boolean isUpdate) {
//获取文件存储权限
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
//下载插件,里面的下载方法可以换成你们自己的,例如okhttp,xutils3等等下载都行,然后在回调中处理那几个方法就行
MMCHttpClient.getInstance(context).downLoadFile(fileUrl, Environment.getExternalStorageDirectory() + "/" + pluginName + ".apk", new MMCHttpClient.OnDownloadListener() {
@Override
public void onDownloadSuccess() {
installPlugin(context, pluginName, activityName, isUpdate);
}
@Override
public void onDownloading(int progress) {
if (installListener != null) {
installListener.onInstalling(progress);
}
}
@Override
public void onDownloadFailed() {
if (installListener != null) {
installListener.onFail("下载失败");
}
}
});
}
/**
* 打开插件的Activity
*
* @param context
* @param pluginName
* @param activityName
*/
public void openActivity(Context context, String pluginName, String activityName) {
RePlugin.startActivity(context, RePlugin.createIntent(pluginName, activityName));
}
/**
* 打开插件的Activity 可带参数传递
*
* @param context
* @param intent
* @param pluginName
* @param activityName
*/
public void openActivity(Context context, Intent intent, String pluginName, String activityName) {
intent.setComponent(new ComponentName(pluginName, activityName));
RePlugin.startActivity(context, intent);
}
/**
* 打开插件的Activity 带回调
*
* @param activity
* @param intent
* @param pluginName
* @param activityName
* @param requestCode
*/
public void openActivityForResult(Activity activity, Intent intent, String pluginName, String activityName, int requestCode) {
intent.setComponent(new ComponentName(pluginName, activityName));
RePlugin.startActivityForResult(activity, intent, requestCode, null);
}
private InstallListener installListener;
public interface InstallListener {
void onInstalling(int progress);
void onFail(String msg);
void onSuccess();
}
}
这样就可以实现外置插件的下载更新打开逻辑,根据自己的实际情况修改下就能用到项目中了吧
网友评论