Android bindService失败,解决方法。

版权声明:本文出自朋永的博客,转载必须注明出处。 https://blog.csdn.net/VNanyesheshou/article/details/79062977

1 Android 5.0以下设备bindService 失败。

首先查看service有没有在AndroidManifest.xml中声明

<service
    android:name=".AIDLService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="com.zpengyong.aidl"></action>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</service>

其次要保证客户端bindservice的intent action和service注册action 一致。

2 Service Intent must be explicit

Android 5.0及以上的设备,google出于安全的角度禁止了隐式声明Intent来启动Service.也禁止使用Intent filter.否则就会抛个异常出来.

java.lang.IllegalArgumentException: Service Intent must be explicit

解决方法:使用显示intent。

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.zpengyong.aidl", "com.zpengyong.aidl.AIDLService"));
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

3 使用AIDL、Messenger 启动另一个进程中的Service失败。

现在比较新的手机中有一个关联启动的设置项,把Service程序的关联启动打开,这样别的进程通过AIDL或Messenger绑定到服务就可以绑定成功了。

猜你喜欢

转载自blog.csdn.net/VNanyesheshou/article/details/79062977