github地址:https://github.com/DroidPluginTeam/DroidPlugin/blob/master/readme_cn.md
关于配置,跟着说明文档配置吧。
关于跳转调用,
使用new Intent(String ActiviyUrl),你会发现,GG,好像不行啊。
原因是你并不能指定Activity,你告知他插件的包名,他从程序入口开始运行,看起来好像是个用来启动APK的东西
一个demo:
手动在存储卡的test目录下放置需要打开的APK,页面点击上面button会把apk加载进去,点击下面Button,会跳转到你想去的apk。
注意:对于加载方法的第二个参数,如果是第一次加载,写0,后面的使用PackageManagerCompat.INSTALL_REPLACE_EXISTING
private void loadPlus(int position) {
progress.setVisibility(View.VISIBLE);
SharedPreferences sharedPreferences = getSharedPreferences("IsFirst", Context.MODE_PRIVATE);
boolean isFirst = sharedPreferences.getBoolean("first", false);
try {
//获取插件,里面的url是你apk的存放路径,实际项目中就是检查插件,然后下载到指定目录
File file = new File(Environment.getExternalStorageDirectory(), "/test/apk" + position);
File[] plugins = file.listFiles();
if (plugins.length == 0) {
Toast.makeText(MainActivity.this, "未发现apk文件", Toast.LENGTH_LONG).show();
progress.setVisibility(View.GONE);
return;
}
int resulet;
final PackageInfo info = getPackageManager().getPackageArchiveInfo(plugins[0].getAbsolutePath(), PackageManager.GET_ACTIVITIES);
if (info == null) {
Toast.makeText(MainActivity.this, "apk损坏!", Toast.LENGTH_SHORT).show();
progress.setVisibility(View.GONE);
return;
}
if(isFirst){
//第一次导入
resulet = PluginManager.getInstance().installPackage(plugins[0].getAbsolutePath(), 0);
}else {
//更新的操作
resulet = PluginManager.getInstance().installPackage(plugins[0].getAbsolutePath(), PackageManagerCompat.INSTALL_REPLACE_EXISTING);
}
switch (resulet) {
case PluginManager.INSTALL_FAILED_NO_REQUESTEDPERMISSION:
Toast.makeText(MainActivity.this, "安装失败,文件请求的权限太多", Toast.LENGTH_SHORT).show();
progress.setVisibility(View.GONE);
break;
case INSTALL_FAILED_NOT_SUPPORT_ABI:
Toast.makeText(MainActivity.this, "宿主不支持插件的abi环境,可能宿主运行时为64位,但插件只支持32位", Toast.LENGTH_SHORT).show();
progress.setVisibility(View.GONE);
break;
case INSTALL_SUCCEEDED:
if(isFirst){
//保存状态
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("first", false);
editor.commit();
}else {
}
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(info.packageName);
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.e("DroidPlugin", "start " + info.packageName + "@" + intent);
startActivity(intent);
} else {
Log.e("DroidPlugin", "pm " + pm.toString() + " no find intent " + info.packageName);
}
progress.setVisibility(View.GONE);
break;
}
} catch (RemoteException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "安装失败,未知异常", Toast.LENGTH_SHORT).show();
progress.setVisibility(View.GONE);
}
}