前言
Binder 驱动是基于 CS 模型设计的跨进程通信驱动, 想要使用 Binder 驱动进行通信, 需要三个步骤
一、 定义交互规范
public interface IService extends IInterface {
String DESCRIPTOR = IService.class.getName();
int TRANSACTION_getServiceName = (IBinder.FIRST_CALL_TRANSACTION + 0);
String getServiceName() throws RemoteException;
}
二、服务端的实现
1、接口 IService 在服务端的实现类
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 本地实现类
public class ServiceBinder extends Binder {
private IService mImpl;
public ServiceBinder() {
mImpl = new ServiceBinder(this);
this.attachInterface(mImpl, mImpl.DESCRIPTOR);
}
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
switch (code) {
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 {
data.writeInterfaceToken(DESCRIPTOR);
mBinderProxy.transact(IService.TRANSACTION_getServiceName, data, replay, 0);
replay.readException();
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增加这样的设计也是为了隐私和安全考虑。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)