您的位置:首页 > 移动开发 > Android开发

Android bindService失败,解决方法。

2018-01-15 20:26 633 查看

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绑定到服务就可以绑定成功了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: