转:Android Aidl的使用_android aidl使用-CSDN博客
一.准备
Parcelable,可以理解成只是把car整个对象在aidl中进行传递,就理解成一个car的一个类吧,和其他类使用一样就行了,回调:把接口作为参数放在函数参数中。接口的函数传递相关信息,注册方式是把接口放在自己的函数中,然后去调用接口的函数。回调方真正实现接口的函数。
用 Aidl 实现跨进程通信的步骤,大致为:
1.确认要传入的数据,序列化数据类;
2.创建 Aidl 接口;
3.服务端 Service 实现 Aidl 的接口;
4.客户端绑定服务端,与服务端通信。
本例包名:com.test.relearnaidl
1.确认要传入的数据,序列化实体数据类
Aidl 支持的数据类型有:
- 八种基本数据类型:byte、char、short、int、long、float、double、boolean
- String,CharSequence
- 实现了Parcelable接口的数据类型
- List类型。List承载的数据必须是AIDL支持的类型,或者是其它声明的AIDL对象
- Map类型。Map承载的数据必须是AIDL支持的类型,或者是其它声明的AIDL对象
AIDL参数TAG 有 in、out、inout 三种,基本类型默认为in,自定义类型需显示声明
- 当out时,需要空参数构造方法
- 当 out或inout 时,自定义类型需手动实现
public void readFromParcel(Parcel desc) {
name = desc.readString();
}
实际开发中,我们要传入的数据可能是 JavaBean 类型的,需要先进行序列化,Android 的序列化推荐用 Parcelable 。
序列化后,本例的 Car 类就可以通过 Aidl 传输了。
-
package com.test.relearnaidl.aidl;
-
import android.os.Parcel;
-
import android.os.Parcelable;
-
import androidx.annotation.NonNull;
-
public class Car implements Parcelable {
-
private int id;
-
private String name;
-
public Car(int id, String name){
-
this.id = id;
-
this.name = name;
-
}
-
protected Car(Parcel in) {
-
id = in.readInt();
-
name = in.readString();
-
}
-
public int getId() {
-
return id;
-
}
-
public void setId(int id) {
-
this.id = id;
-
}
-
public String getName() {
-
return name;
-
}
-
public void setName(String name) {
-
this.name = name;
-
}
-
public static final Creator<Car> CREATOR = new Creator<Car>() {
-
@Override
-
public Car createFromParcel(Parcel in) {
-
return new Car(in);
-
}
-
@Override
-
public Car[] newArray(int size) {
-
return new Car[size];
-
}
-
};
-
@Override
-
public int describeContents() {
-
return 0;
-
}
-
@Override
-
public void writeToParcel(Parcel dest, int flags) {
-
dest.writeInt(id);
-
dest.writeString(name);
-
}
-
@NonNull
-
@Override
-
public String toString() {
-
return String.format("[id:%d, name:%s]",id, name);
-
}
-
}
我们写好参数后,直接代码补全生成序列化的一系列方法了。
2.创建 Aidl 接口
2.1 新建 aidl 文件夹
在 project 视图下,在 main 目录新建 Aidl File ,会生成一个 Aidl 文件夹,用来存放 aidl 文件。
2.2 编写 aidl 接口
新建 ICarManager.aidl 文件, 本例中,只添加获取和增加的接口,
-
// ICarManager.aidl
-
package com.test.relearnaidl.aidl;
-
import com.test.relearnaidl.aidl.Car;
-
// Declare any non-default types here with import statements
-
interface ICarManager {
-
/**
-
* Demonstrates some basic types that you can use as parameters
-
* and return values in AIDL.
-
*/
-
List<Car> getCarList();
-
void addNewCar(in Car car);
-
}
因为我们用的是自定义的 Parcelable 对象 Car,所以还需要编写一个 Car.aidl ,并声明为 Parcelable 类型。
-
package com.test.relearnaidl.aidl;
-
parcelable Car;
需要注意的是,数据类 和 aidl文件 的包的路径(或者说目录结构)要对应上。
本例包名:com.test.relearnaidl ,
我在 java 目录下新建了一个文件夹aidl来存放 Car 类,
所以 aidl 目录下也需要新建 aidl 文件夹来存放这两个 aidl 文件;
否则会报错 couldn't find import for class 。
3.服务端实现
3.1 实现 aidl 接口
其实就是 onBind 中返回一个 Binder 对象,这个 Binder 对象继承 ICarManager.Stub() 并实现了它内部的方法。
-
package com.test.relearnaidl.service;
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.Binder;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import androidx.annotation.Nullable;
-
import com.test.relearnaidl.aidl.Car;
-
import com.test.relearnaidl.aidl.ICarManager;
-
import java.util.List;
-
import java.util.concurrent.CopyOnWriteArrayList;
-
public class CarManagerService extends Service {
-
private CopyOnWriteArrayList<Car> mCarList = new CopyOnWriteArrayList<Car>();
-
private Binder mBinder = new ICarManager.Stub() {
-
@Override
-
public List<Car> getCarList() throws RemoteException {
-
return mCarList;
-
}
-
@Override
-
public void addNewCar(Car car) throws RemoteException {
-
mCarList.add(car);
-
}
-
};
-
@Nullable
-
@Override
-
public IBinder onBind(Intent intent) {
-
return mBinder;
-
}
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
mCarList.add(new Car(100, "SUV 100"));
-
}
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
return super.onStartCommand(intent, flags, startId);
-
}
-
}
3.2 服务端配置
既然要跨进程通信,方然要让 Service 运行在独立进程中,配置下,
-
<service android:name=".service.CarManagerService"
-
android:process=":remote">
-
</service>
还有一点,数据处理,简单的数据可以使用 CopyOnWriteArrayList 或者 ConcurrentHashMap 。
4.客户端实现
写个简单的页面,几个按钮分别是 绑定服务端、获取数据、新增数据。
绑定服务端:用 bindService 的方式,绑定成功后将服务端返回的 Binder 转换成 aidl 接口,这样就可以调用服务端的方法了。
这样,客户端就可以获取和新增数据了。
客户端监听数据变化
如服务端数据有更新,客户端怎么及时知道呢,来探索下。
流程为:
- 1.新增一个 aidl 接口;
- 2.服务端修改;
- 3.客户端注册监听。
1.新增一个 aidl 接口
首先,在 aidl 文件夹下新建一个 aidl 文件 IOnNewCarAddListener.aidl ,
-
package com.test.relearnaidl.aidl;
-
import com.test.relearnaidl.aidl.Car;
-
interface IOnNewCarAddListener{
-
void onNewCarAdd(in Car newCar);
-
}
然后修改 ICarManager.aidl ,添加两个方法
-
// ICarManager.aidl
-
package com.test.relearnaidl.aidl;
-
import com.test.relearnaidl.aidl.Car;
-
import com.test.relearnaidl.aidl.IOnNewCarAddListener;
-
// Declare any non-default types here with import statements
-
interface ICarManager {
-
/**
-
* Demonstrates some basic types that you can use as parameters
-
* and return values in AIDL.
-
*/
-
List<Car> getCarList();
-
void addNewCar(in Car car);
-
void registerListener(IOnNewCarAddListener listener);
-
void unregisterListener(IOnNewCarAddListener listener);
-
}
2.服务端修改
修改的不多,主要是为了适配新加的接口。
- mBinder 对象实现新增的接口;
- 新增一个列表 mListenerList 存储注册的监听器,数据变化时通知到 mListenerList 里所有的 listenerList ;
- 添加一个 addRunnable 模拟数据变化。
-
package com.test.relearnaidl.service;
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.Binder;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import android.util.Log;
-
import androidx.annotation.Nullable;
-
import com.test.relearnaidl.aidl.Car;
-
import com.test.relearnaidl.aidl.ICarManager;
-
import com.test.relearnaidl.aidl.IOnNewCarAddListener;
-
import java.util.List;
-
import java.util.concurrent.CopyOnWriteArrayList;
-
public class CarManagerService extends Service {
-
private CopyOnWriteArrayList<Car> mCarList = new CopyOnWriteArrayList<Car>();
-
private CopyOnWriteArrayList<IOnNewCarAddListener> mListenerList = new CopyOnWriteArrayList<IOnNewCarAddListener>();
-
private Binder mBinder = new ICarManager.Stub() {
-
@Override
-
public List<Car> getCarList() throws RemoteException {
-
return mCarList;
-
}
-
@Override
-
public void addNewCar(Car car) throws RemoteException {
-
mCarList.add(car);
-
onNewCarAdd(car);
-
}
-
@Override
-
public void registerListener(IOnNewCarAddListener listener) throws RemoteException {
-
if (!mListenerList.contains(listener)) {
-
mListenerList.add(listener);
-
} else {
-
Log.d("luoah", "[CarManagerService] -- registerListener -- this listener already exist");
-
}
-
}
-
@Override
-
public void unregisterListener(IOnNewCarAddListener listener) throws RemoteException {
-
if (mListenerList.contains(listener)) {
-
mListenerList.remove(listener);
-
} else {
-
Log.d("luoah", "[CarManagerService] -- unregisterListener -- listener not found");
-
}
-
}
-
};
-
private void onNewCarAdd(Car car) throws RemoteException{
-
mCarList.add(car);
-
for (int j = 0; j < mListenerList.size(); j++){
-
IOnNewCarAddListener listener = mListenerList.get(j);
-
listener.onNewCarAdd(car);
-
}
-
}
-
@Nullable
-
@Override
-
public IBinder onBind(Intent intent) {
-
return mBinder;
-
}
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
mCarList.add(new Car(100, "SUV_100"));
-
new Thread(new addRunnable()).start();
-
}
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
return super.onStartCommand(intent, flags, startId);
-
}
-
private class addRunnable implements Runnable{
-
@Override
-
public void run() {
-
try{
-
Thread.sleep(3000);
-
} catch (InterruptedException ie){
-
ie.printStackTrace();
-
}
-
try {
-
Car bcar = new Car(700, "BM_700");
-
onNewCarAdd(bcar);
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
3.客户端修改
成功连接后注册 IOnNewCarAddListener ,退出时反注册。和广播很像。
-
package com.test.relearnaidl;
-
import androidx.appcompat.app.AppCompatActivity;
-
import android.content.ComponentName;
-
import android.content.Intent;
-
import android.content.ServiceConnection;
-
import android.os.Bundle;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.Button;
-
import com.test.relearnaidl.aidl.Car;
-
import com.test.relearnaidl.aidl.ICarManager;
-
import com.test.relearnaidl.aidl.IOnNewCarAddListener;
-
import com.test.relearnaidl.service.CarManagerService;
-
import java.util.List;
-
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
-
private Button mButtonBind, mButtonGet, mButtonAdd;
-
private ICarManager carManager;
-
private ServiceConnection mConnection = new ServiceConnection() {
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
carManager = ICarManager.Stub.asInterface(service);
-
try {
-
List<Car> conCar = carManager.getCarList();
-
if (conCar != null && conCar.size() > 0) {
-
Log.d("luoah", "[MainActivity] -- onServiceConnected -- conCar:" + conCar.toString());
-
}
-
carManager.registerListener(clientListener);
-
} catch (RemoteException rme) {
-
rme.printStackTrace();
-
}
-
}
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
try {
-
carManager.unregisterListener(clientListener);
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
};
-
private IOnNewCarAddListener clientListener = new IOnNewCarAddListener.Stub() {
-
@Override
-
public void onNewCarAdd(Car newCar) throws RemoteException {
-
Log.d("luoah", "[MainActivity] -- onNewCarAdd -- newCar:" + newCar.toString());
-
}
-
};
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
mButtonBind = (Button)findViewById(R.id.button_bind);
-
mButtonGet = (Button)findViewById(R.id.button_get);
-
mButtonAdd = (Button)findViewById(R.id.button_add);
-
mButtonBind.setOnClickListener(this);
-
mButtonGet.setOnClickListener(this);
-
mButtonAdd.setOnClickListener(this);
-
}
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()){
-
case R.id.button_bind:
-
Intent intent = new Intent(MainActivity.this, CarManagerService.class);
-
bindService(intent, mConnection, BIND_AUTO_CREATE);
-
break;
-
case R.id.button_get:
-
try {
-
List<Car> cars = carManager.getCarList();
-
if (cars != null && cars.size() > 0) {
-
Log.d("luoah", "[MainActivity] -- onClickGet -- cars:" + cars.toString());
-
}
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
break;
-
case R.id.button_add:
-
try {
-
carManager.addNewCar(new Car(200, "SUV_200"));
-
carManager.addNewCar(new Car(300, "SUV_300"));
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
break;
-
default:break;
-
}
-
}
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
if (carManager != null
-
&& carManager.asBinder().isBinderAlive()) {
-
try {
-
carManager.unregisterListener(clientListener);
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
unbindService(mConnection);
-
}
-
}
这样,当数据变化时,客户端就可以收到消息了。
But ,按返回键退出应用,log 是
com.test.relearnaidl D/luoah: [CarManagerService] -- unregisterListener -- listener not found
不是同一个 listener 吗,怎么找不到 ?
原因如下,摘抄自 《Android开发艺术探索》
多进程无法奏效,因为 Binder 会把客户端传过来的对象重新转化成一个新的对象。虽然注册和解注册过程中使用的是同一个客户端对象,但是通过 Binder 传递到服务端后,却会产生两个全新的对象。别忘了对象是不能跨进程传递的,对象的跨进程传递本质上是反序列化的过程,这就是为什么 AIDL 中的自定义对象都必须要实现 Parcelable 接口。
解决这个问题,用 RemoteCallbackList
。
正确添加移除监听
用 RemoteCallbackList 来正确移除注册到服务端的 listener 。
客户端不用修改,修改服务端。
- 使用 RemoteCallbackList 代替 CopyOnWriteArrayList ;
- 修改注册、反注册方法;
- 修改通知 listener 的方法。
1.使用 RemoteCallbackList 代替 CopyOnWriteArrayList
private RemoteCallbackList<IOnNewCarAddListener> mRemoteCallbackList = new RemoteCallbackList<IOnNewCarAddListener>();
2.修改注册、反注册方法;
-
@Override
-
public void registerListener(IOnNewCarAddListener listener) throws RemoteException {
-
mRemoteCallbackList.register(listener);
-
/*if (!mListenerList.contains(listener)) {
-
mListenerList.add(listener);
-
} else {
-
Log.d("luoah", "[CarManagerService] -- registerListener -- this listener already exist");
-
}*/
-
}
-
@Override
-
public void unregisterListener(IOnNewCarAddListener listener) throws RemoteException {
-
mRemoteCallbackList.unregister(listener);
-
/*if (mListenerList.contains(listener)) {
-
mListenerList.remove(listener);
-
} else {
-
Log.d("luoah", "[CarManagerService] -- unregisterListener -- listener not found");
-
}*/
-
}
3.修改通知 listener 的方法
-
private void onNewCarAdd(Car car) throws RemoteException{
-
mCarList.add(car);
-
/*for (int j = 0; j < mListenerList.size(); j++){
-
IOnNewCarAddListener listener = mListenerList.get(j);
-
listener.onNewCarAdd(car);
-
}*/
-
int t = mRemoteCallbackList.beginBroadcast();
-
for (int j = 0; j < t; j++){
-
IOnNewCarAddListener listener = mRemoteCallbackList.getBroadcastItem(j);
-
if (listener != null) {
-
try{
-
listener.onNewCarAdd(car);
-
} catch (RemoteException e){
-
e.printStackTrace();
-
}
-
}
-
}
-
mRemoteCallbackList.finishBroadcast();
-
}
再添加了权限判断,没权限的客户端无法获取到 Binder。
注册文件添加权限,
-
<permission android:name="com.test.relearnaidl.permision.ACCESS_CAR_DATA"
-
android:protectionLevel="normal"/>
服务端 onBind 方法中判断权限,
-
@Nullable
-
@Override
-
public IBinder onBind(Intent intent) {
-
int ticket = checkCallingPermission("com.test.relearnaidl.permision.ACCESS_CAR_DATA");
-
if (ticket == PackageManager.PERMISSION_DENIED) {
-
return null;
-
}
-
return mBinder;
-
}
附上终版源码,方便以后查阅。
工程目录
1.AIDL
ICarManager.aidl
-
// ICarManager.aidl
-
package com.test.relearnaidl.aidl;
-
import com.test.relearnaidl.aidl.Car;
-
import com.test.relearnaidl.aidl.IOnNewCarAddListener;
-
// Declare any non-default types here with import statements
-
interface ICarManager {
-
/**
-
* Demonstrates some basic types that you can use as parameters
-
* and return values in AIDL.
-
*/
-
List<Car> getCarList();
-
void addNewCar(in Car car);
-
void registerListener(IOnNewCarAddListener listener);
-
void unregisterListener(IOnNewCarAddListener listener);
-
}
Car.aidl
-
package com.test.relearnaidl.aidl;
-
parcelable Car;
IOnNewCarAddListener.aidl
-
package com.test.relearnaidl.aidl;
-
import com.test.relearnaidl.aidl.Car;
-
interface IOnNewCarAddListener{
-
void onNewCarAdd(in Car newCar);
-
}
2.服务端
CarManagerService.java
-
package com.test.relearnaidl.service;
-
import android.app.Service;
-
import android.content.Intent;
-
import android.os.Binder;
-
import android.os.IBinder;
-
import android.os.RemoteCallbackList;
-
import android.os.RemoteException;
-
import android.util.Log;
-
import androidx.annotation.Nullable;
-
import com.test.relearnaidl.aidl.Car;
-
import com.test.relearnaidl.aidl.ICarManager;
-
import com.test.relearnaidl.aidl.IOnNewCarAddListener;
-
import java.util.List;
-
import java.util.concurrent.CopyOnWriteArrayList;
-
public class CarManagerService extends Service {
-
private CopyOnWriteArrayList<Car> mCarList = new CopyOnWriteArrayList<Car>();
-
private CopyOnWriteArrayList<IOnNewCarAddListener> mListenerList = new CopyOnWriteArrayList<IOnNewCarAddListener>();
-
private RemoteCallbackList<IOnNewCarAddListener> mRemoteCallbackList = new RemoteCallbackList<IOnNewCarAddListener>();
-
private Binder mBinder = new ICarManager.Stub() {
-
@Override
-
public List<Car> getCarList() throws RemoteException {
-
return mCarList;
-
}
-
@Override
-
public void addNewCar(Car car) throws RemoteException {
-
mCarList.add(car);
-
onNewCarAdd(car);
-
}
-
@Override
-
public void registerListener(IOnNewCarAddListener listener) throws RemoteException {
-
mRemoteCallbackList.register(listener);
-
/*if (!mListenerList.contains(listener)) {
-
mListenerList.add(listener);
-
} else {
-
Log.d("luoah", "[CarManagerService] -- registerListener -- this listener already exist");
-
}*/
-
}
-
@Override
-
public void unregisterListener(IOnNewCarAddListener listener) throws RemoteException {
-
mRemoteCallbackList.unregister(listener);
-
/*if (mListenerList.contains(listener)) {
-
mListenerList.remove(listener);
-
} else {
-
Log.d("luoah", "[CarManagerService] -- unregisterListener -- listener not found");
-
}*/
-
}
-
};
-
private void onNewCarAdd(Car car) throws RemoteException{
-
mCarList.add(car);
-
/*for (int j = 0; j < mListenerList.size(); j++){
-
IOnNewCarAddListener listener = mListenerList.get(j);
-
listener.onNewCarAdd(car);
-
}*/
-
int t = mRemoteCallbackList.beginBroadcast();
-
for (int j = 0; j < t; j++){
-
IOnNewCarAddListener listener = mRemoteCallbackList.getBroadcastItem(j);
-
if (listener != null) {
-
try{
-
listener.onNewCarAdd(car);
-
} catch (RemoteException e){
-
e.printStackTrace();
-
}
-
}
-
}
-
mRemoteCallbackList.finishBroadcast();
-
}
-
@Nullable
-
@Override
-
public IBinder onBind(Intent intent) {
-
int ticket = checkCallingPermission("com.test.relearnaidl.permision.ACCESS_CAR_DATA");
-
if (ticket == PackageManager.PERMISSION_DENIED) {
-
return null;
-
}
-
return mBinder;
-
}
-
@Override
-
public void onCreate() {
-
super.onCreate();
-
Log.d("luoah", "[CarManagerService] -- onCreate -- ");
-
mCarList.add(new Car(100, "SUV_100"));
-
new Thread(new addRunnable()).start();
-
}
-
@Override
-
public int onStartCommand(Intent intent, int flags, int startId) {
-
return super.onStartCommand(intent, flags, startId);
-
}
-
private class addRunnable implements Runnable{
-
@Override
-
public void run() {
-
try{
-
Thread.sleep(3000);
-
} catch (InterruptedException ie){
-
ie.printStackTrace();
-
}
-
try {
-
Car bcar = new Car(700, "BM_700");
-
onNewCarAdd(bcar);
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
3.客户端
客户端 MainActivity.java 。
目前都是在主线程操作的,实际使用的话,还是用 Handler 等方式来获取数据比较好,防止出现超时出现 ANR 等情况。
-
package com.test.relearnaidl;
-
import androidx.appcompat.app.AppCompatActivity;
-
import android.content.ComponentName;
-
import android.content.Intent;
-
import android.content.ServiceConnection;
-
import android.os.Bundle;
-
import android.os.IBinder;
-
import android.os.RemoteException;
-
import android.util.Log;
-
import android.view.View;
-
import android.widget.Button;
-
import com.test.relearnaidl.aidl.Car;
-
import com.test.relearnaidl.aidl.ICarManager;
-
import com.test.relearnaidl.aidl.IOnNewCarAddListener;
-
import com.test.relearnaidl.service.CarManagerService;
-
import java.util.List;
-
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
-
private Button mButtonBind, mButtonGet, mButtonAdd;
-
private ICarManager carManager;
-
private ServiceConnection mConnection = new ServiceConnection() {
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder service) {
-
carManager = ICarManager.Stub.asInterface(service);
-
try {
-
List<Car> conCar = carManager.getCarList();
-
if (conCar != null && conCar.size() > 0) {
-
Log.d("luoah", "[MainActivity] -- onServiceConnected -- conCar:" + conCar.toString());
-
}
-
carManager.registerListener(clientListener);
-
} catch (RemoteException rme) {
-
rme.printStackTrace();
-
}
-
}
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
try {
-
carManager.unregisterListener(clientListener);
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
};
-
private IOnNewCarAddListener clientListener = new IOnNewCarAddListener.Stub() {
-
@Override
-
public void onNewCarAdd(Car newCar) throws RemoteException {
-
Log.d("luoah", "[MainActivity] -- onNewCarAdd -- newCar:" + newCar.toString());
-
}
-
};
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
mButtonBind = (Button)findViewById(R.id.button_bind);
-
mButtonGet = (Button)findViewById(R.id.button_get);
-
mButtonAdd = (Button)findViewById(R.id.button_add);
-
mButtonBind.setOnClickListener(this);
-
mButtonGet.setOnClickListener(this);
-
mButtonAdd.setOnClickListener(this);
-
}
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()){
-
case R.id.button_bind:
-
Intent intent = new Intent(MainActivity.this, CarManagerService.class);
-
bindService(intent, mConnection, BIND_AUTO_CREATE);
-
break;
-
case R.id.button_get:
-
try {
-
List<Car> cars = carManager.getCarList();
-
if (cars != null && cars.size() > 0) {
-
Log.d("luoah", "[MainActivity] -- onClickGet -- cars:" + cars.toString());
-
}
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
break;
-
case R.id.button_add:
-
try {
-
carManager.addNewCar(new Car(200, "SUV_200"));
-
carManager.addNewCar(new Car(300, "SUV_300"));
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
break;
-
default:break;
-
}
-
}
-
@Override
-
protected void onDestroy() {
-
super.onDestroy();
-
if (carManager != null
-
&& carManager.asBinder().isBinderAlive()) {
-
try {
-
carManager.unregisterListener(clientListener);
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
unbindService(mConnection);
-
}
-
}
4.数据
就是 Car 类了
-
package com.test.relearnaidl.aidl;
-
import android.os.Parcel;
-
import android.os.Parcelable;
-
import androidx.annotation.NonNull;
-
public class Car implements Parcelable {
-
private int id;
-
private String name;
-
public Car(int id, String name){
-
this.id = id;
-
this.name = name;
-
}
-
protected Car(Parcel in) {
-
id = in.readInt();
-
name = in.readString();
-
}
-
public int getId() {
-
return id;
-
}
-
public void setId(int id) {
-
this.id = id;
-
}
-
public String getName() {
-
return name;
-
}
-
public void setName(String name) {
-
this.name = name;
-
}
-
public static final Creator<Car> CREATOR = new Creator<Car>() {
-
@Override
-
public Car createFromParcel(Parcel in) {
-
return new Car(in);
-
}
-
@Override
-
public Car[] newArray(int size) {
-
return new Car[size];
-
}
-
};
-
@Override
-
public int describeContents() {
-
return 0;
-
}
-
@Override
-
public void writeToParcel(Parcel dest, int flags) {
-
dest.writeInt(id);
-
dest.writeString(name);
-
}
-
@NonNull
-
@Override
-
public String toString() {
-
return String.format("[id:%d, name:%s]",id, name);
-
}
-
}
5.注册文件和布局文件
注册文件
-
<?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:tools="http://schemas.android.com/tools"
-
package="com.test.relearnaidl">
-
<permission android:name="com.test.relearnaidl.permision.ACCESS_CAR_DATA"
-
android:protectionLevel="normal"/>
-
<application
-
android:allowBackup="true"
-
android:icon="@mipmap/ic_launcher"
-
android:label="@string/app_name"
-
android:roundIcon="@mipmap/ic_launcher_round"
-
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=".service.CarManagerService"
-
android:process=":remote">
-
</service>
-
</application>
-
</manifest>
布局文件
-
<?xml version="1.0" encoding="utf-8"?>
-
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
-
android:layout_height="match_parent"
-
tools:context=".MainActivity">
-
<Button
-
android:id="@+id/button_bind"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="bind servcie"
-
app:layout_constraintBottom_toBottomOf="parent"
-
app:layout_constraintHorizontal_bias="0.494"
-
app:layout_constraintLeft_toLeftOf="parent"
-
app:layout_constraintRight_toRightOf="parent"
-
app:layout_constraintTop_toTopOf="parent"
-
app:layout_constraintVertical_bias="0.625" />
-
<Button
-
android:id="@+id/button_get"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="get car list"
-
app:layout_constraintBottom_toBottomOf="parent"
-
app:layout_constraintHorizontal_bias="0.498"
-
app:layout_constraintLeft_toLeftOf="parent"
-
app:layout_constraintRight_toRightOf="parent"
-
app:layout_constraintTop_toTopOf="parent"
-
app:layout_constraintVertical_bias="0.733" />
-
<Button
-
android:id="@+id/button_add"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="add new Car"
-
app:layout_constraintBottom_toBottomOf="parent"
-
app:layout_constraintHorizontal_bias="0.494"
-
app:layout_constraintLeft_toLeftOf="parent"
-
app:layout_constraintRight_toRightOf="parent"
-
app:layout_constraintTop_toTopOf="parent"
-
app:layout_constraintVertical_bias="0.838" />
-
</androidx.constraintlayout.widget.ConstraintLayout>
使用linkToDeath对AIDL双向死亡监听
概述
在使用service中进行AIDL交互时候,如果服务端或者客户端意外停止,会抛出异常android.os.DeadObjectException , 这时候我们就需要双向监听服务端和客户端的异常停止,并重新绑定服务
linkToDeath的使用
linkToDeath 为Binder对象添加死亡代理。
unlinkToDeath 取消死亡代理
客户端
首先我们需要创建一个DeathRecipient对象
-
private IBinder.DeathRecipient mDeathProxy = new IBinder.DeathRecipient() {
-
@Override
-
public void binderDied() {
-
//监听死亡,重新绑定
-
Log.d("zkq", "服务端崩溃,需要重新绑定");
-
mBinder.asBinder().unlinkToDeath(mDeathProxy, 0);
-
}
-
};
在service绑定成功以后,给服务绑定死亡代理 ,当服务端异常停止以后,我们会收到binderDied回调,并在这里重新绑定服务
-
private ServiceConnection conn = new ServiceConnection() {
-
@Override
-
public void onServiceConnected(ComponentName name, IBinder iBinder) {
-
Log.d("zkq", " onServiceConnected = " + iBinder);
-
mBinder = IMyAidlInterface.Stub.asInterface(iBinder);
-
try {
-
mBinder.asBinder().linkToDeath(mDeathProxy, 0);
-
}catch (RemoteException e){
-
Log.e("zkq", "onServiceConnected: linkToDeath error");
-
}
-
try {
-
mBinder.setCallback(mICallBack);
-
} catch (RemoteException e) {
-
e.printStackTrace();
-
}
-
}
-
@Override
-
public void onServiceDisconnected(ComponentName name) {
-
Log.e("zkq", " onServiceDisconnected = " + name);
-
}
-
};
绑定服务
-
private void bind() {
-
Intent intent = new Intent();
-
intent.setAction(MyService.ACTION);
-
intent.setPackage(getPackageName());
-
boolean ret = bindService(intent, conn, Context.BIND_AUTO_CREATE);
-
Log.d("zkq", " ret = " + ret);
-
}
服务端
相应的在服务端,添加客户端的死亡监听,我们只需要在binderDied中,移除客户端的回调
-
IMyAidlInterface.Stub myAidlInterface = new IMyAidlInterface.Stub() {
-
@Override
-
public void setCallback(ICallBack var1) throws RemoteException {
-
var1.asBinder().linkToDeath(new DeathRecipient() {
-
@Override
-
public void binderDied() {
-
Log.d("zkq","客户端崩溃,移除回调");
-
mBinder.asBinder().unlinkToDeath(this, 0);
-
}
-
},0);
-
}
-
};
完整服务端代码如下:
-
public class MyService extends Service {
-
public static final String ACTION = "com.zkq.autofly.action.START_SERVICE";
-
IMyAidlInterface mBinder;
-
public MyService() {
-
}
-
IMyAidlInterface.Stub myAidlInterface = new IMyAidlInterface.Stub() {
-
@Override
-
public void setCallback(ICallBack var1) throws RemoteException {
-
var1.asBinder().linkToDeath(new DeathRecipient() {
-
@Override
-
public void binderDied() {
-
Log.d("zkq","客户端崩溃,需要重新绑定");
-
}
-
},0);
-
}
-
};
-
@Override
-
public IBinder onBind(Intent intent) {
-
if (intent.getAction().equals(ACTION)) {
-
return myAidlInterface;
-
}
-
return null;
-
}