AIDL报错,bindService一直连接不上、不起作用。

84 篇文章 5 订阅
67 篇文章 2 订阅

先说问题,最近用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文件,按照谷歌要求注册需要使用的包名:如下所示【注意<queries>这个标签】

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mzz_client">

    <queries>
        <!--如果想要与其他的应用进行AIDL通信的话,需要在这里注册包名的信息-->
        <!--谷歌的文档说明 https://developer.android.google.cn/guide/topics/manifest/queries-element?hl=en-->
        <package android:name="com.example.mzz_service" />
    </queries>
    <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/Theme.MyUtils">
        <activity
            android:name=".Activity_Main"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".LocalService" />
    </application>

</manifest>

原因是因为在高版本上面,使用其他应用的服务这种方式,谷歌做了更严格的限制措施:

如果有想看谷歌关于这块的说明的话,请跳转谷歌关于调用其他应用的说明https://developer.android.google.cn/guide/topics/manifest/queries-element?hl=en

  • 22
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 16
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值