如何使用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()[].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初体验的更多相关文章
- .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
不知不觉,“.NET平台开源项目速览“系列文章已经15篇了,每一篇都非常受欢迎,可能技术水平不高,但足够入门了.虽然工作很忙,但还是会抽空把自己知道的,已经平时遇到的好的开源项目分享出来.今天就给大家 ...
- Xamarin+Prism开发详解四:简单Mac OS 虚拟机安装方法与Visual Studio for Mac 初体验
Mac OS 虚拟机安装方法 最近把自己的电脑升级了一下SSD固态硬盘,总算是有容量安装Mac 虚拟机了!经过心碎的安装探索,尝试了国内外的各种安装方法,最后在youtube上找到了一个好方法. 简单 ...
- Spring之初体验
Spring之初体验 Spring是一个轻量级的Java Web开发框架,以IoC(Inverse of Control 控制反转)和 ...
- Xamarin.iOS开发初体验
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKwAAAA+CAIAAAA5/WfHAAAJrklEQVR4nO2c/VdTRxrH+wfdU84pW0
- 【腾讯Bugly干货分享】基于 Webpack & Vue & Vue-Router 的 SPA 初体验
本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57d13a57132ff21c38110186 导语 最近这几年的前端圈子,由于 ...
- 【Knockout.js 学习体验之旅】(1)ko初体验
前言 什么,你现在还在看knockout.js?这货都已经落后主流一千年了!赶紧去学Angular.React啊,再不赶紧的话,他们也要变out了哦.身旁的90后小伙伴,嘴里还塞着山东的狗不理大蒜包, ...
- 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验
在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...
- 百度EChart3初体验
由于项目需要在首页搞一个订单数量的走势图,经过多方查找,体验,感觉ECharts不错,封装的很细,我们只需要看自己需要那种类型的图表,搞定好自己的json数据就OK.至于说如何体现出来,官网的教程很详 ...
- Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验
Python导出Excel为Lua/Json/Xml实例教程(二):xlrd初体验 相关链接: Python导出Excel为Lua/Json/Xml实例教程(一):初识Python Python导出E ...
- Docker初体验
## Docker初体验 安装 因为我用的是mac,所以安装很简单,下载dmg下来之后拖拽安装即可完成. 需要注意的就是由于之前的docker是基于linux开发,不支持mac,所以就出现了docke ...
随机推荐
- synchronized重入后抛出异常,锁释放了吗
synchronized: 用于同步方法或者代码块,使得多个线程在试图并发执行同一个代码块的时候,串行地执行.以达到线程安全的目的. 允许重入: 在多线程的时候是这样的,但是对于单线程,是允许重入的, ...
- Quartz 定时器,同时运用多个定时器
效果:每天执行两个定时器,两个定时器不相关联.jar版本Quartz 2.2.3 Java工程结构图 jar 包下载: 链接: https://pan.baidu.com/s/1-7dh620k9P ...
- codevs 1664 清凉冷水
1664 清凉冷水 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 闷热的夏天,威斯康辛州的奶制品地区提供冷水供奶牛 ...
- oracle 查询之前的表数据
SELECT * FROM Student AS OF TIMESTAMP SYSDATE - 3/1440 对SQL的解释说明: SYSDATE :当前时间 1440 :24h*60m=1440m ...
- C#开发android应用实战 源码
原书名: Professional Android Programming with Mono for Android and .NET/C# Download Title Size Down ...
- cms-友情链接实现静态化
service: package com.open1111.service.impl; import java.util.List; import javax.servlet.ServletConte ...
- html常用的小技能
在html中有很多常用小技能,记下来,下次直接看自己的,就不用四处找啦! 1.<li>标签去掉点号:list-style-type:none; 去掉前: 去掉后: 2.<li> ...
- firefox 提示 ssl_error_unsupported_version 的解决方法
访问一些HTTPS网站时尤其是国内网站 中文提示: 无法安全地连接 Firefox 无法保证您在 sx.ac.10086.cn 上的数据安全性,因为它使用 SSLv3,一个目前安全性欠佳的安全协议.专 ...
- PHP中MySQL数据库连接,数据读写,修改方法
MySQL连接大的来说有两种方法,一种是mysqli,另一种是mysql.php为连接MySQL提供了函数库,有mysql和mysqli,mysqli是mysql函数库的扩展,是php5才支持的.当你 ...
- Android(java)学习笔记153:采用post请求提交数据到服务器(qq登录案例)
1.POST请求: 数据是以流的方式写给服务器 优点:(1)比较安全 (2)长度不限制 缺点:编写代码比较麻烦 2.我们首先在电脑模拟下POST请求访问服务器的场景: 我们修改之前编写的logi ...