aidl使用采坑记
什么是AIDL?
AIDL是 Android Interface definition language的缩写,它是一种Android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
AIDL可以解决什么问题?
- 可以实现多个应用程序共享同一个Service的功能,比如:IM服务可以提供给多个APP使用,先在推送基本都是采取这种方案
- 可以跨进程调用服务里的方法
搭建了简单的Service框架
1.继承Service
public class PushService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
}
2.在AndroidManifest.xml里注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="name.quanke.aidldemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".App"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".PushService"
android:enabled="true"
android:process=":push"
android:exported="true">
</service>
</application>
</manifest>
|
建立AIDL
创建AIDL文件
编写AIDL文件
interface IHandler {
void connect();
}
aidl生成后的样子
编写客户端 ServiceConnection
public class PushClient {
private IHandler iHandler;
private static PushClient instance = new PushClient();
public static PushClient getInstance() {
return instance;
}
public void init(Application app){
Intent binderIntent = new Intent(app,PushService.class);
app.bindService(binderIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iHandler = IHandler.Stub.asInterface(service);
//连接成功调用
}
@Override
public void onServiceDisconnected(ComponentName name) {
//断开连接调用
}
};
//通过AIDL远程调用
public void connect(){
try {
iHandler.connect();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
写服务端实现connect方法
public class LibHandler extends IHandler.Stub{
@Override
public void connect() throws RemoteException {
}
@Override
public IBinder asBinder() {
return null;
}
}
测试
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
findViewById(R.id.aidl_test).setOnClickListener(this);
}
@Override
public void onClick(View view) {
PushClient.getInstance().connect();
}
}
其实到这里我们就结束了。
传递自定义的类型
AIDL默认支持的类型包括Java基本类型(int、long、boolean等),和(String、List、Map、CharSequence),如果要传递自定义的类型需要实现android.os.Parcelable接口。
public class Message implements Parcelable {
private long id;
private String content;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Message{" +
"id=" + id +
", content='" + content + '\'' +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(this.id);
dest.writeString(this.content);
}
protected Message(Parcel in) {
this.id = in.readLong();
this.content = in.readString();
}
public static final Creator<Message> CREATOR = new Creator<Message>() {
@Override
public Message createFromParcel(Parcel source) {
return new Message(source);
}
@Override
public Message[] newArray(int size) {
return new Message[size];
}
};
}
修改IHandler
interface IHandler {
void connect();
void sendMessage(Message message);
}
sendMessage方法的message参数是纯粹的输入参数—这意味着是从客户端到服务器的数据,你需要在AIDL声明:void sendMessage(in Message message);
如果sendMessage方法的message参数是纯粹的输出-这意味着它的数据是通过从服务器到客户端,使用:
void sendMessage(out Message message);
如果sendMessage方法的message参数是输入也是输出-客户端的值在服务可能会修改,使用:
void sendMessage(inout Message message);
我们这里是客户端范围服务端的数据,所以用in
interface IHandler {
void connect();
void sendMessage(in Message message);
}
好了,aidl的用法就到这里了,aidl主要是用在跨进程间通信和数据交换,平时开发中也用的比较少,通过这个例子加深了对他的用法,后面有什么好的坑,我会发出了的,谢谢大家。最后做个链接
https://github.com/xiangzhihong/aidl
aidl使用采坑记的更多相关文章
- 分布式改造剧集之Redis缓存采坑记
Redis缓存采坑记 前言 这个其实应该属于分布式改造剧集中的一集(第一集见前面博客:http://www.cnblogs.com/Kidezyq/p/8748961.html),本来按照顺序 ...
- Spring Cloud Config采坑记
1. Spring Cloud Config采坑记 1.1. 问题 在本地运行没问题,本地客户端服务能连上本地服务端服务,可一旦上线,发现本地连不上线上的服务 服务端添加security登录加密,客户 ...
- k8s采坑记 - 解决二进制安装环境下证书过期问题
前言 上一篇k8s采坑记 - 证书过期之kubeadm重新生成证书阐述了如何使用kubeadm解决k8s证书过期问题. 本篇阐述使用二进制安装的kubernetes环境,如何升级过期证书? k8s配置 ...
- Redis适配采坑记
Redis适配采坑记 相对于其他的适配,Redis可以说是非常简单的其中只发现一个坑 问题一: 问题描述: redis认证失败 问题详解: redis连接配置时,本地需要采用password属性,远程 ...
- tk.mybatis通用工具采坑记
tk.mybatis通用工具pom <!--mybatis依赖--> <dependency> <groupId>org.mybatis.spring.boot&l ...
- dubbo初学采坑记
写在前面的话 dubbo 现在是apache组织旗下的项目,相信国内也有很多人使用.最近一个同事离职,我就接手了他的项目.远程通讯就是用的dubbo框架来实现的.使用Intelij idea 写了一个 ...
- Service worker (@nuxtjs/workbox) 采坑记
PWA(Progressive Web App)是前端的大趋势,它能极大的加快前端页面的加载速度,得到近乎原生 app 的展示效果(其实难说).PWA 其实是多种前端技术的组合,其中最重要的一个技术就 ...
- css选择器:first-child和nth-child 采坑记
今天想用nth-child来给一个类似于树的目录(bootstrap-nav-tree 一个angularjs插件)设置不同的颜色,结构大致类似于 <ul> <li class=& ...
- 使用wepy框架搭建微信小程序采坑记(一)
1.什么是wepy 这个框架是腾讯内部出的一个类MVVM的小程序开发框架.大体上来说语法是类VUE的,所以如果有VUE开发经验的话迁移成本会低一些.至于具体的怎么使用我就不赘言了,有问题查文档(官方文 ...
随机推荐
- ACM Meteor Shower
贝茜听到一场非同寻常的流星雨( meteor shower)即将来临;有报道称这些流星将撞击地球并摧毁它们所击中的任何东西.为了安全起见(Anxious for her safety), ,她发誓(v ...
- MySQL 连接的使用
MySQL 连接的使用 在前几章节中,我们已经学会了如果在一张表中读取数据,这是相对简单的,但是在真正的应用中经常需要从多个数据表中读取数据. 本章节我们将向大家介绍如何使用 MySQL 的 JOIN ...
- 粗浅看Struts2和Hibernate框架
----------------------------------------------------------------------------------------------[版权申明: ...
- move_uploaded_file的failed to open stream错误处理
PHP的基本语法学习的差不多了,现在开始学习PHP的文件上传功能实现了.功能中使用到了move_uploaded_file方法,运行时报错: failed to open stream. 经过查资料, ...
- 计算机网络之万维网WWW
万维网 WWW (World Wide Web)并非某种特殊的计算机网络,而是一个大规模的.联机式的信息储藏所. 万维网用链接的方法能非常方便地从因特网上的一个站点访问另一个站点,从而主动地按需获取丰 ...
- log file sync 因为数据线有问题而造成高等侍的表现
这是3月份某客户的情况,原因是服务器硬件故障后进行更换之后,业务翻译偶尔出现提交缓慢的情况.我们先来看下awr的情况. 我们可以看到,该系统的load profile信息其实并不高,每秒才21个tra ...
- 将String转换为其表示的路径画到屏幕上
关于这个问题,我已经在另一篇blog中有所提及: CoreText精彩文字轮廓绘制动画的一点改进 不过原有的转换代码使用Obj-C写的,在这里我们尝试将其转换为Swift语言,然后利用它实现一个测试小 ...
- Scikit-learn:模型评估Model evaluation
http://blog.csdn.net/pipisorry/article/details/52250760 模型评估Model evaluation: quantifying the qualit ...
- Swift中的as操作符
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交 ...
- 3.QT中的debug相关的函数,以及文件锁的使用
1 新建项目T33Debug main.cpp #include <QDebug> #include <QFile> #include <QMutex> ...