关于AIDL通讯在魅族16Xs的兼容性问题

关于AIDL通讯在魅族16Xs的兼容性问题

最近在项目中遇到一个AIDL兼容魅族手机(16Xs,16s)问题,记录一下,以便下次后续翻阅。

1.设定好统一接口ICpMessage
2.程序A(com.xxx.xxxA)设定AIDL需要绑定的服务, 在AndroidManifest.xml中声明服务。


3.程序B(com.xxx.xxxB)(对外提供的SDK包)绑定程序A所提供的服务

Intent intent = new Intent();
intent.setClassName("com.xxx.xxxA", "com.xxx.xxxA.XxxAidlService");
activity.bindService(intent, mConn, Context.BIND_AUTO_CREATE);

具体问题如下:
程序B(SDK)在调用程序A时,无法进行服务的绑定,但是可以调起程序A。

在华为,三星,Vivo, Oppo等手机(也包含Android 9.0)均表现正常,唯独在魅族的16Xs,16s(Android 9.0)无法进行绑定,出现如下异常:

03-31 22:15:54.681 I/Interception( 1590): third procedure runType: retrieve_service caller: com.xxx.xxxB callee: com.xxx.xxxA fg: true processName: com.xxx.xxxA calleeCalss: com.xxx.xxxA.XxxAidlService intent: Intent { cmp=com.xxx.xxxA/.XxxAidlService }
03-31 22:15:54.681 D/Interception( 1590): V4 isInterceptByRule realCallingPid: 8041 isMatch: false, runType=retrieve_service|||callerPkg=com.xxx.xxxB|||calleePkg=com.xxx.xxxA|||calleeClass=com.xxx.xxxA.XxxAidlService|||processName=com.xxx.xxxA|||action=|||data===|||extras===*|||interaction=fg|||intercept result=0
03-31 22:15:54.681 W/Interception( 1590): Link isInterceptAsUsual, caller com.xxx.xxxB call com.xxx.xxxA interception default intercept as true!
03-31 22:15:54.681 W/Interception( 1590): reason:6)finally, intercept as usual, interception = true details<>

看到log中第一行,intent: Intent { cmp=com.xxx.xxxA/.XxxAidlService },觉得并无异常,component已经是正确的了,所以不觉得setClassName()有问题。
第二行的log,明确说明了callee 的pid not match(isMatch:false), 但是在程序运行后检查pid与log提示的pid 也就是说问题出在了intent的参数上面,所以才会出现几第三行和第四行的intercept.
接着修改intent, 将packageName,className使用ComponentName进行设置传递,添加的时候没有把握,所以添加了setPackage()。具体如下:

Intent intent = new Intent();
intent.setPackage("com.xxx.xxxA");
ComponentName componentName = new ComponentName("com.xxx.xxxA", "com.xxx.xxxA.XxxAidlService");
intent.setComponent(componentName);
activity.bindService(intent, mConn, Context.BIND_AUTO_CREATE);

修改之后,sdk却依然不能唤起服务。
参考网上一些文章进行分析,怀疑可能是魅族OEM软体对该bindService的动作进行了限制(Android原生:bindService动作会检索所有程序包含的服务(不管是否启动),如果服务未启动,通过判断Context.BIND_AUTO_CREATE的设置,进行启动。)
而魅族的OEM软体可能对此作了修改,使Context.BIND_AUTO_CREATE无效,也就是说,只能对已经启动的服务进行bindService操作。
带着这个想法,在程序A的application->onCreate()函数中添加服务的启动代码:

startService(new Intent(this, XxxAidlService.class));

修改之后,sdk就能兼容魅族16Xs和16s.

总结:对于兼容性适配问题,多分析给出的log,在确保代码无误的情况下,可以对framework的修改进行假设,尝试对应的修改来对自己的假设进行验证。

你可能感兴趣的:(Android)