前言
Binder 驱动是基于 CS 模型设计的跨进程通信驱动, 想要使用 Binder 驱动进行通信, 需要三个步骤
- 定义交互规范
- 服务端实现
- 客户端实现
一、 定义交互规范
public interface IService extends IInterface {
/*
跨进程提供服务的接口描述
*/
String DESCRIPTOR = IService.class.getName();
/*
跨进程提供服务的接口中 getServiceName 这个方法 Transaction id.
*/
int TRANSACTION_getServiceName = (IBinder.FIRST_CALL_TRANSACTION + 0);
String getServiceName() throws RemoteException;
}
二、服务端的实现
1、接口 IService 在服务端的实现类
/**
* 接口 IService 在服务端的实现类
* {@link #mBinder } 这个对象是让 IService 拥有跨进程提供数据能力的 Binder 本地实现类
*/
public class ServiceImpl implements IService {
private IBinder mBinder;
public ServiceImpl(IBinder binder) {
this.mBinder = binder;
}
@Override
public String getServiceName() {
return "This this IService support function.";
}
@Override
public IBinder asBinder() {
return mBinder;
}
}
2、Service 端 Binder 本地实现类
/**
* Service 端 Binder 本地实现类
* 其向外提供的功能接口为 {@link IService}
*/
public class ServiceBinder extends Binder {
/*
持有其对应接口实现类的引用
*/
private IService mImpl;
public ServiceBinder() {
mImpl = new ServiceBinder(this);
// 调用了 attachInterface 之后, 父类 Binder 将会持有当前 IInterface 接口的描述
// 在 onTransact 中 会自动处理 INTERFACE_TRANSACTION 类型的事务
// 在 queryLocalInterface 中可以找到本地接口
this.attachInterface(mImpl, mImpl.DESCRIPTOR);
}
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
switch (code) {
// 调用了 attachInterface 之后, 父类会处理该类型的 code.
// case INTERFACE_TRANSACTION: {
// reply.writeString(DESCRIPTOR);
// return true;
// }
case IService.TRANSACTION_getServiceName: {
data.enforceInterface(mImpl.DESCRIPTOR);
reply.writeNoException();
reply.writeString(mImpl.getServiceName());
return true;
}
default:
return super.onTransact(code, data, reply, flags);
}
}
}
三、客户端的实现
public class ServiceProxyImpl implements IService {
private IBinder mBinderProxy;
public ServiceProxyImpl(IBinder binder) {
mBinderProxy = binder;
}
@Override
public String getServiceName() throws RemoteException {
String result = null;
Parcel data = Parcel.obtain();
Parcel replay = Parcel.obtain();
try {
// 1. 写入调用方法所对应接口的描述
data.writeInterfaceToken(DESCRIPTOR);
// 2. 向 Service 端发起 invocation 请求
mBinderProxy.transact(IService.TRANSACTION_getServiceName, data, replay, 0);
// 3. 读取 Service 端扔出来的异常信息
replay.readException();
// 4. 读取异常结果
result = replay.readString();
} finally {
replay.recycle();
data.recycle();
}
return result;
}
@Override
public IBinder asBinder() {
return mBinderProxy;
}
}
四、具体实现
1、服务端
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new ServiceBinder();
}
}
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.mi.mytest.yobo" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
2、客户端
Intent intent = new Intent();
intent.setPackage("com.mi.mytest");
intent.setAction("com.mi.mytest.yobo");
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e("yobo","onServiceConnected");
iService = new ServiceProxyImpl(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("yobo","onServiceDisconnected");
}
}, BIND_AUTO_CREATE);
3、注意
- IService、ServiceBinder、ServiceImpl、ServiceProxyImpl需要在同一应用包名下
- Android11以后对于启动别的应用的Activity和Service做了隐藏功能,需要在客户端添加如下代码:
<queries>
<intent>
<action android:name="com.mi.mytest.yobo" />
</intent>
</queries>
五、Android 11上Google收缩了程序包可见性
其实是Android 11上Google收缩了程序包可见性,这就导致可能查询不到三方应用及其内部的ContentProvider、Service组件。Android开发者官网给出的解释如下:
“如果您的应用以 Android 11(API 级别 30)或更高版本为目标平台,在默认情况下,系统会自动让部分应用对您的应用可见,但会隐藏其他应用。通过让部分应用在默认情况下不可见,系统可以了解应向您的应用显示哪些其他应用,这样有助于鼓励最小权限原则,还可帮助 Google Play 等应用商店评估应用为用户提供的隐私权和安全性。”
1、降低版本兼容处理(不推荐)
首先想到的办法就是,降低targetSdkVersion版本,这样也行,运行在Android 11设备上没问题,不过迟早还是需要适配Android这个特性的。
targetSdkVersion 29
2、增加QUERY_ALL_PACKAGES权限(不建议)
记得曾在Android 11适配的过程中用到新增的一个权限:android.permission.QUERY_ALL_PACKAGES。加上这个权限就能够查询所有的应用,解决前面无法启动三方应用Service的问题。而且QUERY_ALL_PACKAGES权限等级为normal,只要在AndroidManifest.xml中申请声明即可获得该权限。
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
不过,Google限制该权限的使用,如果开发者的应用需要上架Google Play,最好不要使用该权限否则会被检测警告甚至不让上架。
3、使用标签声明使用的三方组件(推荐)
可以使用范围更具体的的查询,使用标签声明要查询的,系统会查询所有能够处理该Intent的Service等组件。
<queries>
<intent>
<action android:name="cn.quibbler.server.aidl" />
<data android:path="quibbler" />
</intent>
</queries>
需要注意,开发者不要在中声明“宽泛的intent”。
对于ContentProvider,通过中的子标签查询指定的authorities。
<queries>
<provider android:authorities="sadas" />
</queries>
还直接添加合作的三方应用包名,使对方包对自己可见。
<queries>
<package android:name="com.tencent.qq" />
</queries>
开发者应当遵循最小使用原则,只添加应用所需的三方应用包,Google增加这样的设计也是为了隐私和安全考虑。