集成起来很简单。在这里我也只是尝尝鲜而已实际。项目中并没有用到
截图先上:
DroidPlugin项目地址:https://github.com/DroidPluginTeam/DroidPlugin
开始集成:
-
我们只需要将Droid Plugin当作一个lib工程应用到主项目中,然后:
-
在
AndroidManifest.xml
中使用插件的com.morgoo.droidplugin.PluginApplication
:<application android:name="com.morgoo.droidplugin.PluginApplication" android:label="@string/app_name" android:icon="@drawable/ic_launcher"
-
如果你使用自定义的
Application
,那么你需要在自定义的应用程序类onCreate
和attachBaseContext
方法中添加如下代码:@Override public void onCreate() { super.onCreate(); //这里必须在super.onCreate方法之后,顺序不能变 PluginHelper.getInstance().applicationOnCreate(getBaseContext()); } @Override protected void attachBaseContext(Context base) { PluginHelper.getInstance().applicationAttachBaseContext(base); super.attachBaseContext(base); }
-
修改
Libraries\DroidPlugin\build.gradle
的defaultConfig配置中authorityName
的值(建议改为自己的包名+标识,防止跟其它本插件使用者冲突) -
集成完成。
安装一个插件:
[java] 查看纯文本
- PluginManager.getInstance()。installPackage(file.getAbsolutePath(),PackageManagerCompat.INSTALL_REPLACE_EXISTING);
卸载一个插件:
[java] 查看纯文本
- PluginManager.getInstance()。deletePackage(PLUGIN_PACKAGE_NAME, 0 );
启动插件:
[java] 查看纯文本
- PackageManager pm = getPackageManager();
- Intent intent = pm.getLaunchIntentForPackage(PLUGIN_PACKAGE_NAME);
- if (null != intent){
- startActivity(意向);
- } else {
- Toast.makeText(getApplicationContext(), “插件还未安装” ,Toast.LENGTH_SHORT).show();
- }
完整代码:
[java] 查看纯文本
- package com.example.gk.testpluginhost;
- 导入 android.content.Intent;
- import android.content.pm.ApplicationInfo;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- 导入 android.os.Environment;
- 导入 android.os.RemoteException;
- 导入 android.support.v7.app.AppCompatActivity;
- 导入 android.os.Bundle;
- 导入 android.util.Log;
- 导入 android.view.View;
- 导入 android.widget.Button;
- 导入 android.widget.TextView;
- 导入 android.widget.Toast;
- import com.morgoo.droidplugin.pm.PluginManager;
- import com.morgoo.helper.compat.PackageManagerCompat;
- import java.io.File;
- import java.util.List;
- / **
- *测试插件化
- * /
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- 私人 TextView txt_title;
- 私人 按钮btn_check;
- 私人 按钮btn_install;
- 私人 按钮btn_uninstall;
- 私人 按钮btn_open;
- public static final String PLUGIN_PACKAGE_NAME = “com.kk.imgod.testcustomview” ;
- @覆盖
- protected void onCreate(Bundle savedInstanceState){
- 超级.onCreate(savedInstanceState);
- 的setContentView(R.layout.activity_main);
- initViews();
- }
- private void initViews(){
- txt_title =(TextView)findViewById(R.id.txt_title);
- btn_check =(Button)findViewById(R.id.btn_check);
- btn_install =(Button)findViewById(R.id.btn_install);
- btn_uninstall =(Button)findViewById(R.id.btn_uninstall);
- btn_open =(Button)findViewById(R.id.btn_open);
- btn_check.setOnClickListener(this );
- btn_install.setOnClickListener(this );
- btn_uninstall.setOnClickListener(this );
- btn_open.setOnClickListener(this );
- }
- @覆盖
- public void onClick(View v){
- switch (v.getId()){
- case R.id.btn_check:
- 检查();
- 打破;
- case R.id.btn_install:
- 安装();
- 打破;
- case R.id.btn_uninstall:
- 卸载();
- 打破;
- 案例 R.id.btn_open:
- openPlugin();
- 打破;
- 默认值:
- 打破;
- }
- }
- / **
- *检查是否已经安装了插件
- * /
- private void check(){
- Intent intent = getLaunchehIntent();
- //通过这种方法来判断插件是不是安装不准确,毕竟如果插件是安装在手机里面的话也不为空
- //精准的方法是通过下面插件管理里面的方法,然后再判断
- if (null != intent){
- Toast.makeText(getApplicationContext(), “当前插件已经安装” ,Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(getApplicationContext(), “当前插件还未安装” ,Toast.LENGTH_SHORT).show();
- }
- 尝试 {
- List <ApplicationInfo> appList = PluginManager.getInstance()。getInstalledApplications(0 );
- if (null != appList){
- Log.e(“test” , “installed app:” + appList.size());
- for (ApplicationInfo applicationInfo:appList){
- Log.e(“test_app” ,applicationInfo.packageName);
- }
- }
- } catch (RemoteException e){
- e.printStackTrace();
- }
- 尝试 {
- 列表<PackageInfo> packageList = PluginManager.getInstance()。getInstalledPackages(0 );
- if (null != packageList){
- Log.e(“test” , “installed package:” + packageList.size());
- for (PackageInfo packageInfo:packageList){
- Log.e(“test_package” ,packageInfo.packageName);
- }
- }
- } catch (RemoteException e){
- e.printStackTrace();
- }
- }
- / **
- *安装插件
- * /
- private void install(){
- //获取插件
- File file = new File(Environment.getExternalStorageDirectory(), “/apppp/app/test_plugin.apk” );
- //没有插件
- if (file.exists()){
- Log.e(“test” , “插件文件存在” );
- int installResult = 0 ;
- 尝试 {
- installResult = PluginManager.getInstance()。installPackage(file.getAbsolutePath(),PackageManagerCompat.INSTALL_REPLACE_EXISTING);
- } catch (RemoteException e){
- e.printStackTrace();
- Log.e(“test” , “插件文件安装失败” );
- }
- Log.e(“test” , “插件文件安装成功:installResult:” + installResult);
- if (installResult == PackageManagerCompat.INSTALL_SUCCEEDED){
- Toast.makeText(getApplicationContext(), “插件安装成功” ,Toast.LENGTH_SHORT).show();
- } else {
- Toast.makeText(getApplicationContext(), “插件安装失败:” + installResult,Toast.LENGTH_SHORT).show();
- }
- } else {
- Log.e(“test” , “插件文件不存在” );
- }
- }
- / **
- *卸载插件
- * /
- private void unInstall(){
- //获取插件
- //没有插件
- 尝试 {
- PluginManager.getInstance()。deletePackage(PLUGIN_PACKAGE_NAME, 0 );
- Toast.makeText(getApplicationContext(), “插件卸载成功” ,Toast.LENGTH_SHORT).show();
- } catch (RemoteException e){
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), “插件卸载失败” ,Toast.LENGTH_SHORT).show();
- }
- }
- / **
- *打开插件
- * /
- private void openPlugin(){
- Intent intent = getLaunchehIntent();
- if (null != intent){
- // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //经测试不用这个flag也行
- startActivity(意向);
- Log.e(“test” , “插件已经启动” );
- } else {
- Toast.makeText(getApplicationContext(), “插件还未安装” ,Toast.LENGTH_SHORT).show();
- }
- }
- / **
- *拿到意图
- *
- * @返回
- * /
- private Intent getLaunchehIntent(){
- PackageManager pm = getPackageManager();
- Intent intent = pm.getLaunchIntentForPackage(PLUGIN_PACKAGE_NAME);
- 回报 意向
- }
- }