集成起来很简单.在这里我也只是尝尝鲜而已.实际项目中并没有用到

截图先上:



DroidPlugin项目地址:https://github.com/DroidPluginTeam/DroidPlugin

开始集成:

  1. 我们只需要将Droid Plugin当作一个lib工程应用到主项目中,然后:

  2. AndroidManifest.xml中使用插件的com.morgoo.droidplugin.PluginApplication

  3. 如果你使用自定义的Application,那么你需要在自定义的Application class onCreateattachBaseContext方法中添加如下代码:

    @Overridepublic void onCreate() {    super.onCreate();    //这里必须在super.onCreate方法之后,顺序不能变    PluginHelper.getInstance().applicationOnCreate(getBaseContext());}@Overrideprotected void attachBaseContext(Context base) {    PluginHelper.getInstance().applicationAttachBaseContext(base);    super.attachBaseContext(base);}
  4. 修改 Libraries\DroidPlugin\build.gradle 的 defaultConfig 配置中 authorityName 的值(建议改为自己的包名+标识,防止跟其它本插件使用者冲突)

  5. 集成完成。

把我们的plugin apk(简单的helloworld都可以)放到指定目录.

安装一个插件:

PluginManager.getInstance().installPackage(file.getAbsolutePath(), PackageManagerCompat.INSTALL_REPLACE_EXISTING);

卸载一个插件:

 PluginManager.getInstance().deletePackage(PLUGIN_PACKAGE_NAME, 0);

启动插件:

 PackageManager pm = getPackageManager();        Intent intent = pm.getLaunchIntentForPackage(PLUGIN_PACKAGE_NAME);        if (null != intent) {            startActivity(intent);        } else {            Toast.makeText(getApplicationContext(), "插件还未安装", Toast.LENGTH_SHORT).show();        }

完整代码:

package com.example.gk.testpluginhost;import android.content.Intent;import android.content.pm.ApplicationInfo;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.os.Environment;import android.os.RemoteException;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import 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 {    private TextView txt_title;    private Button btn_check;    private Button btn_install;    private Button btn_uninstall;    private Button btn_open;    public static final String PLUGIN_PACKAGE_NAME = "com.kk.imgod.testcustomview";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.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);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_check:                check();                break;            case R.id.btn_install:                install();                break;            case R.id.btn_uninstall:                unInstall();                break;            case R.id.btn_open:                openPlugin();                break;            default:                break;        }    }    /**     * 检查是否已经安装了插件     */    private void check() {        Intent intent = getLaunchehIntent();        //通过这种方法来判断插件是不是安装不准确,毕竟如果插件是安装在手机里面的话也不为空        //精准的方法是通过下面PluginManager里面的方法,然后再判断        if (null != intent) {            Toast.makeText(getApplicationContext(), "当前插件已经安装", Toast.LENGTH_SHORT).show();        } else {            Toast.makeText(getApplicationContext(), "当前插件还未安装", Toast.LENGTH_SHORT).show();        }        try {            List 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();        }        try {            List 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;            try {                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() {        //获取插件        //没有插件        try {            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(intent);            Log.e("test", "插件已经启动");        } else {            Toast.makeText(getApplicationContext(), "插件还未安装", Toast.LENGTH_SHORT).show();        }    }    /**     * 拿到Intent     *     * @return     */    private Intent getLaunchehIntent() {        PackageManager pm = getPackageManager();        Intent intent = pm.getLaunchIntentForPackage(PLUGIN_PACKAGE_NAME);        return intent;    }}


集成还是很简单的

demo地址:https://github.com/imgod1/TestPluginHost

更多相关文章

  1. ArchLinux安装Android(安卓)Studio出现的小问题
  2. 解决小米手机Android(安卓)Studio安装app 报错的问题It is possi
  3. android中R.id突然消失
  4. Installation failed with message INSTALL_FAILED_CONFLICTING_
  5. Android(安卓)SDK4.2 (API17) 开发环境的搭建
  6. Android(安卓)Studio 安装No JVM Installation found. Please in
  7. Android(安卓)Studio查看数据库插件
  8. Windows下用Eclipse搭建Android开发环境搭建
  9. Android(安卓)APP中卸载其他APP的三种方法

随机推荐

  1. android Service之四:传递复杂数据类型的
  2. Android——TabWidget(切换卡)
  3. Android(安卓)控件布局常用属性
  4. android开发笔记之android studio
  5. Android(安卓)控件(button)对齐方法实现
  6. Android(安卓)四大组件的理解
  7. Android---网络编程之Retrofit2整体结构
  8. android 牛人必修 ant 编译android工程
  9. Android(安卓)利用 xml 文件实现 ImageVi
  10. Android(安卓)相对布局 RelativeLayout