最近研究了下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()[].getPath());
int result = PluginManager.getInstance().installPackage(files
.listFiles()[].getPath(), );
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, ) != 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服务。

如何使用DroidPlugin——DroidPlugin初体验的更多相关文章

  1. .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验

    不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...

  2. Xamarin+Prism开发详解四:简单Mac OS 虚拟机安装方法与Visual Studio for Mac 初体验

    Mac OS 虚拟机安装方法 最近把自己的电脑升级了一下SSD固态硬盘,总算是有容量安装Mac 虚拟机了!经过心碎的安装探索,尝试了国内外的各种安装方法,最后在youtube上找到了一个好方法. 简单 ...

  3. Spring之初体验

                                     Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...

  4. Xamarin.iOS开发初体验

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKwAAAA+CAIAAAA5/WfHAAAJrklEQVR4nO2c/VdTRxrH+wfdU84pW0

  5. 【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57d13a57132ff21c38110186 导语 最近这几年的前端圈子,由于 ...

  6. 【Knockout.js 学习体验之旅】(1)ko初体验

    前言 什么,你现在还在看knockout.js?这货都已经落后主流一千年了!赶紧去学Angular.React啊,再不赶紧的话,他们也要变out了哦.身旁的90后小伙伴,嘴里还塞着山东的狗不理大蒜包, ...

  7. 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验

    在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...

  8. 百度EChart3初体验

    由于项目需要在首页搞一个订单数量的走势图,经过多方查找,体验,感觉ECharts不错,封装的很细,我们只需要看自己需要那种类型的图表,搞定好自己的json数据就OK.至于说如何体现出来,官网的教程很详 ...

  9. Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验

    Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...

  10. Docker初体验

    ## Docker初体验 安装 因为我用的是mac,所以安装很简单,下载dmg下来之后拖拽安装即可完成. 需要注意的就是由于之前的docker是基于linux开发,不支持mac,所以就出现了docke ...

随机推荐

  1. jQuery学习笔记(三)

    jQuery中的事件 页面加载 原生DOM中的事件具有页面加载的内容onload事件,在jQuery中同样提供了对应的内容ready()函数. ready与onload之间的区别: onload re ...

  2. Servlet高级部分Listener

    监听器的使用场景: ①:统计在线人数   ②:实现单一登录[一个账号只能在一台机器上登录] Servlet中的8大监听器: 1.         ServletContextListener [接口方 ...

  3. weex 项目开发 weexpack 项目 打包、签名、发布

    一. weexpack build android  和  weexpack run android 的 区别. (1)单纯打包 weexpack build android (2)打包并运行 wee ...

  4. Python学习笔记-day1(if流程控制)

    在python中,流程控制语句为强制缩进(4空格) if username=='lmc' and password=='123456': print('Welcome User {name} logi ...

  5. c语言数组赋值

    int acct_parm[3] = {4, 2, 30};#define RESUME (acct_parm[0])

  6. datagrid数据表格使用总结

    一.加载的css文件 easyui 基本样式: <link href="../easyui/easyui1.5.css" rel="stylesheet" ...

  7. 介绍一款渗透神器——Burp Suite

    Burp Suite 是用于攻击web 应用程序的集成平台.它包含了许多工具,并为这些工具设计了许多接口,以促进加快攻击应用程序的过程.所有的工具都共享一个能处理并显示HTTP 消息,持久性,认证,代 ...

  8. COGS 1453. [USACO NOV]空牛栏

    ★★   输入文件:empty.in   输出文件:empty.out   简单对比时间限制:1 s   内存限制:64 MB [题目描述] FJ建的新牛棚里有N(2<=N<=3,000, ...

  9. IOS UIActivityIndicatorView动画

    ● 是一个旋转进度轮,可以用来告知用户有一个操作正在进行中,一般 用initWithActivityIndicatorStyle初始化 ● 方法解析: ● - (void)startAnimating ...

  10. yield 生成器的运行机制

    yield 生成器的运行机制 当你问生成器要一个数时,生成器会执行,直至出现 yield 语句,生成器把 yield 的参数给你,之后生成器就不会往下继续运行. 当你问他要下一个数时,他会从上次的状态 ...