一、AIDL: Android Interface Definition Language,即Android接口定义语言。Android系统中的进程之间不能[共享内存]因此,需要提供一些机制在不同进程之间进行数据通信。
二、AIDL的创建:
1、服务端 服务端首先要创建一个Service用来监听客户端的连接请求,然后创建一个AIDL文件,将暴露给客户端的接口在这个AIDL文件中声明,最后在Service中实现这个AIDL接口即可。
(一)创建一个项目,作为服务进程
(二)创建一个AIDL接口,取名为IMessageAidl
(三)声明暴露给客户端的接口,比如代码中的sendMessage
package com.chaos.serviceclient;
// Declare any non-default types here with import statements
interface IMessageAidl {
String sendMessage(String msg);
}
复制代码
(注意):AIDL文件并不是所有数据类型都支持,它支持以下六中类型 **1.**基本数据类型(int、long、char、boolean、double等); **2.**String和CharSequence; **3.**List:只支持ArrayList,里面每个元素都必须能够被AIDL支持; **4.**Map:只支持HashMap,里面每个元素都必须能够被AIDL支持; **5.**Parcelable:实现了Parcelable接口的对象; **6.**AIDL:所有AIDL接口本身也可以在AIDL文件中使用。
(四)定义好接口后 同步一下项目,系统会自动在这个目录下生成一个文件
(五)服务端创建一个服务,并实现AIDL接口,由于我是手动创建需要在清单文件中声明。
**
* Created by yc.Zhao on 2017/10/26 0026.
* 服务端创建一个Service ,实现AIDL接口
*/
public class MessageService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
private IBinder iBinder = new IMessageAidl.Stub(){
@Override
public String sendMessage(String msg) throws RemoteException {
//处理一下返回给客户端的信息,demo比较简单,直接return了。
return "这里是服务端返回的信息"+msg;
}
};
}
复制代码
清单文件声明,由于是跨进程绑定Service需要在清单文件中赋予intent-filter
<service android:name=".MessageService">
<intent-filter>
<action android:name="com.chaos.serviceclient.MessageService"/>
</intent-filter>
</service>
复制代码
(6)至此,服务端的搭建已经完成。项目目录如下所示。
2、客户端: 绑定服务端的Service,将服务端的返回的Binder对象转成AIDL接口所属的类型,接着就可以调用AIDL中的方法了。
(一)创建一个客户端进程。New Module 我起名字为ClientAidl
(二)在客户端进程中,创建AIDL接口必须和服务端的AIDL包名和接口位置一模一样才可以,要不然运行程序的时候会抛异常,在这里我直接把服务端aidl包拷贝到客户端,保证了 包名和内容一致。
(三)和服务端一样,同步程序,让系统在客户端自动生成AIDL文件
(四)在客户端MainActivity中绑定服务,实现AIDL接口获取服务端传回的数据
1、客户端MainActivity的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chaos.clientaidl.MainActivity">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="客户端发送信息"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送信息"/>
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="接收来自服务的信息"/>
</LinearLayout>
复制代码
2、客户端MainActivity实现代码,注解写的很详细
public class MainActivity extends AppCompatActivity implements View.OnClickListener, ServiceConnection {
private EditText editText;
private Button button;
private TextView textView;
private IMessageAidl iMessageAidl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//绑定服务
initService();
}
private void initService() {
Intent intent = new Intent();
//设置意图
intent.setAction("com.chaos.serviceaidl.ServiceAboutAidl");
//5.0显式启动
intent.setComponent(new ComponentName("com.chaos.serviceaidl","com.chaos.serviceaidl.ServiceAboutAidl"));
//MainActivity实现了ServiceConnection接口
bindService(intent,this, Context.BIND_AUTO_CREATE);
}
private void initView() {
editText = (EditText) findViewById(R.id.et);
button = (Button) findViewById(R.id.btn);
textView = (TextView) findViewById(R.id.tv);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
try {
//由于获取另个进程消息 ,要抛异常
String send = iMessageAidl.send(editText.getText().toString());
textView.setText(send);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
}
//由于MainActivity实现了ServiceConnection接口,在接口中给AIDL的IMessageAidl实例化
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMessageAidl = IMessageAidl.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
iMessageAidl = null;
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(this);
}
}
复制代码
(五)至此客户端处理已经完毕,项目结构图如下
3、运行Demo如下图所示: 注意:运行客户端的时候,必须保证服务端已经安装了(服务端可以不处于运行状态)
至此一个简单的AIDL例子就搭建完毕了,理解了AIDL的构建思路才可以去更好的运用AIDL去跨进程处理一些复杂的业务需求。
如果您觉得对您有帮助,给我一个赞我就很知足了!