DroidPlugin框架简单应用

关于DroidPlugin的具体介绍请到Github地址:https://github.com/Qihoo360/DroidPlugin

关于DroidPlugin在自己项目中的集成的主要步骤:

  • 到上面地址下载DroidPlugin
  • 在androidstudio中新建一个project
  • 在project中导入DroidPlugin库,并建立依赖
  • 修改DroidPlugin库AndroidManifest.xml所有provider中的authorities
  • 修改DroidPlugin库中PluginManager里的STUB_AUTHORITY_NAME
  • 修改宿主AndroidManifest.xml里application的name为com.morgoo.droidplugin.PluginApplication
  • 把做好的插件放到手机文件中(插件就是一个我们平时安装的apk文件)
  • 宿主中安装使用插件

集成

导入库
导入库

导入之后记得修改库
这里写图片描述
编译版本改成和宿主(app)里的一样

建立依赖
这里写图片描述

修改修改DroidPlugin库AndroidManifest.xml所有provider中的authorities,只要和原先的有所区别就行,我这里是用自己的包名。
这里写图片描述

修改完上面的之后记得要修改DroidPlugin库中PluginManager里的STUB_AUTHORITY_NAME,改成和provider中的authorities的一样
这里写图片描述
后面那个_P0几不用写在这边

修改宿主(app)AndroidManifest.xml里application的name为com.morgoo.droidplugin.PluginApplication

这里写图片描述

集成的话到这里就可以了。

放入并使用插件

把apk文件放到手机中

先把插件apk放在assets中,在启动手机的时候写到宿主app的安装目录下。

这里写图片描述

代码

在这边只是测试代码,并未优化

package com.monker.droidplugintest;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by wyy on 2016/7/27.
 */
public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new Thread(new Runnable() {
            @Override
            public void run() {
                writeToCard();
            }
        }).start();
    }


    private void writeToCard() {

        File dir = new File("data/data/com.monker.droidplugintest/plugin");
        if (!dir.exists()) {
            dir.mkdir();
        }

        File file = new File(dir.getAbsolutePath(), "plug-debug.apk");
        InputStream is = null;
        OutputStream out = null;
        try {
            is = getAssets().open("plug-debug.apk");
            out = new FileOutputStream(file);

            byte[] buf = new byte[1024 * 1024];
            int length;
            while ((length = is.read(buf)) != -1) {
                out.write(buf, 0, length);
                out.flush();
            }

            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);


        } catch (IOException e) {
            e.printStackTrace();
        }

        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

}

插件安装并使用

package com.monker.droidplugintest;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.morgoo.droidplugin.pm.PluginManager;
import com.morgoo.helper.compat.PackageManagerCompat;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private File[] plugPaths;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = (TextView) findViewById(R.id.tv_plug);


        //获取插件
        File file = new File("data/data/com.monker.droidplugintest/plugin");
        plugPaths = file.listFiles();
        //没有插件
        if (plugPaths == null || plugPaths.length == 0) {
            return;
        }
        //安装第一个插件
        else {
            try {    PluginManager.getInstance().installPackage(plugPaths[0].getAbsolutePath(), PackageManagerCompat.INSTALL_REPLACE_EXISTING);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
            textView.setText(plugPaths[0].getAbsolutePath());
        }

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PackageManager pm = getPackageManager();
                Intent intent = pm.getLaunchIntentForPackage("com.monker.plug");//插件包名
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);//跳转到插件MAIN界面
            }
        });
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/tv_plug"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="button" />
</RelativeLayout>

宿主app跳转到插件的制定界面,要用隐式跳转,上面的方式只能跳转到主界面

 PackageManager pm = getPackageManager();
                Intent intent = pm.getLaunchIntentForPackage("com.monker.plug");//插件包名
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);//跳转到插件MAIN界面

要跳转到其他界面,应该这么做:
在插件的AndroidManifest.xml注册界面时,记得加上action和category ,action随便自己写一个,在宿主app中要用到。

 <activity android:name=".PlugActivity">
            <intent-filter>
                <action android:name="android.intent.action.start"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

在宿主app中 ,使用插件指定界面的action,就可以了

Intent intent = new Intent();
                intent.setAction("android.intent.action.start");
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

这里写图片描述


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值