先说问题,最近用aidl,发现bindService根本就不走,不知道咋回事,明明写的没有任何毛病啊?
//aidl绑定
private void bind() {
Intent intent = new Intent();
String pkg = "com.example.mzz_service";//需要调用的服务端【另一个APP】的包名
String name = "com.example.mzz_service.service.Remote_Service";
intent.setComponent(new ComponentName(pkg, name));
// intent.setPackage(pkg);
// String action = "mazhanzhu";
// intent.setAction(action);
// Intent intent = new Intent(this, LocalService.class);
bindService = bindService(intent, serviceConnection, BIND_AUTO_CREATE);
Log.e(TAG, "是否绑定成功: " + bindService);
}
昨天找了一天多,今天早上,才找到解决办法,其实不是咱们自己写的有问题,出问题就特么在谷歌!高版本上面,原来的那套逻辑,谷歌单独做了处理
解决方式有两种
第一种方式:
把你项目的 targetSdkVersion 版本修改到30以下。原来的那种方式即可解决。
第二种方式:【推荐】
targetSdkVersion在30及以上:在自己的项目的manifest文件,按照谷歌要求注册需要使用的包名:如下所示【注意
package="com.example.mzz_client"> 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/Theme.MyUtils"> android:name=".Activity_Main" android:exported="true">
原因是因为在高版本上面,使用其他应用的服务这种方式,谷歌做了更严格的限制措施:
如果有想看谷歌关于这块的说明的话,请跳转谷歌关于调用其他应用的说明https://developer.android.google.cn/guide/topics/manifest/queries-element?hl=en