如何使用DroidPlugin——DroidPlugin初体验

最近研究了下360的黑科技--DroidPlugin

刚开始不知道怎么用,于是看了这篇博客:http://www.jianshu.com/p/f1217cce93ef  算是引导了我,于是开始自己写写代码,真正试一把。

我是从两种方式来写的,第一个是把DroidPlugin当成库工程引入的,第二个是把DroidPlugin打成Jar包来使用的,两种方式都成功了。

第一种方式比较简单方便吧,从gitbub上把DroidPlugin下载下来,在AS中通过 import Module 导入,然后在主工程中(默认是app)的目录下的build.gradle的dependencies中加入:

compile project(':DroidPlugin')

加好了之后,gradle(Sync now)一把,就可以了,DroidPlugin被加载成库工程了。

看我的:



第二种方式,先要把DroidPlugin打成jar包,可以参考:http://www.cnblogs.com/IT-Goddess/p/5420682.html 

打成的jar包只包含类文件,一些用的资源文件是打不进去的,所以要将DroidPlugin用的一些资源文件,复制到宿主中,详细的到后面再说。

接下来我先讲第一种方式的(DEMO是参考别人,然后做了自己的修改)。

先建一个类,名为DPApplication,继承Application,内容如下:

public class DPApplication extends Application {
    @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);
    }
}

建好了之后,在AndroidManifest.xml中,把Application节点的name改成“.DPApplication”,如下

<?xml version="1.0" encoding="utf-8"?>
<manifest package="clwang.chunyu.me.wcl_droid_plugin_demo"
          xmlns:android="http://schemas.android.com/apk/res/android">
    <application
        android:name=".DPApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <action android:name="me.chunyu.clwang.master.action_main"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

以上过程是在宿主程序中的,基本的配置工作就这样了。

现在我要实现的是:在宿主中开启插件的某个服务

宿主的MainActivity如下:

public class MainActivity extends AppCompatActivity {
    Button btn_plugin_start;
    Button btn_plugin_install;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_plugin_install = (Button) findViewById(R.id.btn_plugin_install);
        btn_plugin_start = (Button) findViewById(R.id.btn_plugin_start);

btn_plugin_install.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!PluginManager.getInstance().isConnected()) { //return "连接失败"; // 连接失败 Toast.makeText(MainActivity.this, "连接失败", Toast.LENGTH_SHORT).show(); } try { final File files = Environment.getExternalStoragePublicDirectory(Environment .DIRECTORY_DOWNLOADS); new Thread(new Runnable() { @Override public void run() { try { Log.i("============", files.listFiles()[0].getPath()); int result = PluginManager.getInstance().installPackage(files .listFiles()[0].getPath(), 0); Log.i("=============++++++++", result + ""); } catch (RemoteException e) { e.printStackTrace(); } } }).start(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "安装失败", Toast.LENGTH_SHORT).show(); } } }); btn_plugin_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { gotoPlugin(btn_plugin_start); } }); } private void gotoPlugin(View view) { if (isServiceAvailable(view.getContext(), PluginConsts.PLUGIN_ACTION_SERVICE)) { //启动service Intent intent = new Intent(PluginConsts.PLUGIN_ACTION_SERVICE); startService(intent); } else { Toast.makeText(view.getContext(), "打开失败", Toast.LENGTH_SHORT).show(); } } public static boolean isServiceAvailable(Context context, String action) { Intent intent = new Intent(action); return context.getPackageManager().resolveService(intent, 0) != null; } }
这个PluginConsts类是这样的:
/**
 * 插件的常量
 * <p/>
 * Created by wangchenlong on 16/1/15.
 */
public class PluginConsts {
//Service
public static final String PLUGIN_ACTION_SERVICE = "me.chunyu.clwang.plugin.action_service"; }

宿主中就是这样的,接下来看下插件:

插件中有个服务,叫PluginService,如下:

public class PluginService extends Service {
    public static final String TAG = "PluginService";
    String  newId;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand() executed");return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy() executed");
        Toast.makeText(this,"plugin onDestroy() executed",Toast.LENGTH_SHORT).show();
    }
}

然后在插件的AndroidManifest.xml文件中注册下这个服务:

<service android:name=".PluginService">
     <intent-filter>
          <action android:name="me.chunyu.clwang.plugin.action_service"></action>
     </intent-filter>
</service>

把插件APK放到sdcard下的Download文件夹中,到这就可以了,先安装插件,在开启插件的PluginService服务。

原文地址:https://www.cnblogs.com/IT-Goddess/p/5443686.html