未整理版。。。
目前RePlugin2.1.7版本宿主APK包含了这四个如图的主要功能
1. 开启插件demo1中的MainActivity
2. 带返回ForResult的开启插件demo1中的MainActivity
3. 利用demo1中的fragment替换host中fragment
4. 模拟外置插件,安装并开启demo3中的MainActivity
RePlugin.startActivity(MainActivity.this, RePlugin.createIntent("com.qihoo360.replugin.sample.demo1", "com.qihoo360.replugin.sample.demo1.MainActivity"));
可以看到RePlugin.startActivity(Context context, Intent intent) 其实跟我们通常用的startActivity的参数是一致。
public static Intent createIntent(String pluginName, String cls) {
Intent in = new Intent();
in.setComponent(createComponentName(pluginName, cls));
return in;
}
实际包名的设置依赖于
new ComponentName(pluginName, cls) //pluginName 插件名,cls 目标组件全名
Intent intent = new Intent();
intent.setComponent(new ComponentName("demo1", "com.qihoo360.replugin.sample.demo1.activity.for_result.ForResultActivity"));
RePlugin.startActivityForResult(MainActivity.this, intent, REQUEST_CODE_DEMO1, null);
看看demo1中ForResultActivity的写法
Intent intent = new Intent();
intent.putExtra("data", "data from demo1 plugin, resultCode is 0x012");
setResult(0x012, intent);
finish();
其实跟我们常规的开启Activity的写法都是一样的。很简单的实现了两个APK之间值的传递。
public class PluginFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//注册相关Fragment的类
//注册一个全局Hook用于拦截系统对XX类的寻找定向到Demo1中的XX类主要是用于在xml中可以直接使用插件中的类
RePlugin.registerHookingClass("com.qihoo360.replugin.sample.demo1.fragment.DemoFragment", RePlugin.createComponentName("demo1", "com.qihoo360.replugin.sample.demo1.fragment.DemoFragment"), null);
setContentView(R.layout.activity_plugin_fragment);
//代码使用插件Fragment
ClassLoader d1ClassLoader = RePlugin.fetchClassLoader("demo1");//获取插件的ClassLoader
try {
Fragment fragment = d1ClassLoader.loadClass("com.qihoo360.replugin.sample.demo1.fragment.DemoCodeFragment").asSubclass(Fragment.class).newInstance();//使用插件的Classloader获取指定Fragment实例
getSupportFragmentManager().beginTransaction().add(R.id.container2, fragment).commit();//添加Fragment到UI
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
simulateInstallExternalPlugin(PLUGIN_NAME);
pd.dismiss();
}
}, 1000);
/**
* 模拟安装外置插件
* 注意:为方便演示,外置插件临时放置到Host的assets/external目录下,具体说明见README
*
* @param pluginName 待安装的插件名
*/
private void simulateInstallExternalPlugin(String pluginName) {
PluginInfo info = RePlugin.getPluginInfo(pluginName);
if (info == null) {
String pluginFilePath = getFilesDir().getAbsolutePath() + File.separator + PLUGIN_APK;
File pluginFile = new File(pluginFilePath);
if (!pluginFile.exists()) {
copyAssetsFileToAppFiles(PLUGIN_PATH, PLUGIN_APK);
if (pluginFile.exists()) {
info = RePlugin.install(pluginFilePath);
}
}
}
if (info != null) {
RePlugin.startActivity(MainActivity.this, RePlugin.createIntent(info.getName(), "com.qihoo360.replugin.sample.demo3.MainActivity"));
} else {
Toast.makeText(MainActivity.this, "install external plugin failed", Toast.LENGTH_SHORT).show();
}
}