当前位置: 代码迷 >> Android >> 在BroadcastReceiver中bindService失误
  详细解决方案

在BroadcastReceiver中bindService失误

热度:42   发布时间:2016-05-01 17:41:34.0
在BroadcastReceiver中bindService出错
大家好,我写了一个音乐播放器 什么功能都加上去了除了加个widget 话说我在写这个widget的时候我想控制service 其中在bindService的时候crash了 
Java code
07-27 13:31:53.022: E/AndroidRuntime(6028): FATAL EXCEPTION: main07-27 13:31:53.022: E/AndroidRuntime(6028): java.lang.RuntimeException: Unable to start receiver com.xrlfy.service.MusicPlayerWidgetProvider: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to bind to services07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2058)07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.app.ActivityThread.access$2400(ActivityThread.java:132)07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1098)07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.os.Handler.dispatchMessage(Handler.java:99)07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.os.Looper.loop(Looper.java:150)07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.app.ActivityThread.main(ActivityThread.java:4293)07-27 13:31:53.022: E/AndroidRuntime(6028):     at java.lang.reflect.Method.invokeNative(Native Method)07-27 13:31:53.022: E/AndroidRuntime(6028):     at java.lang.reflect.Method.invoke(Method.java:507)07-27 13:31:53.022: E/AndroidRuntime(6028):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)07-27 13:31:53.022: E/AndroidRuntime(6028):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)07-27 13:31:53.022: E/AndroidRuntime(6028):     at dalvik.system.NativeStart.main(Native Method)07-27 13:31:53.022: E/AndroidRuntime(6028): Caused by: android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to bind to services07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.app.ReceiverRestrictedContext.bindService(ContextImpl.java:166)07-27 13:31:53.022: E/AndroidRuntime(6028):     at com.xrlfy.service.MusicPlayerWidgetProvider.bindService(MusicPlayerWidgetProvider.java:169)07-27 13:31:53.022: E/AndroidRuntime(6028):     at com.xrlfy.service.MusicPlayerWidgetProvider.onReceive(MusicPlayerWidgetProvider.java:44)07-27 13:31:53.022: E/AndroidRuntime(6028):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2043)07-27 13:31:53.022: E/AndroidRuntime(6028):     ... 10 more


希望能给我说一下如何在broadcastReceiver中bindService

------解决方案--------------------
Note: this method can not be called from a BroadcastReceiver component. A pattern you can use to communicate from a BroadcastReceiver to a Service is to call startService(Intent) with the arguments containing the command to be sent, with the service calling its stopSelf(int) method when done executing that command. See the API demo App/Service/Service Start Arguments Controller for an illustration of this. It is okay, however, to use this method from a BroadcastReceiver that has been registered with registerReceiver(BroadcastReceiver, IntentFilter), since the lifetime of this BroadcastReceiver is tied to another object (the one that registered it).

bindService不能在BroadcastReceiver 中调用,你可以在里面调用StartService并把要传递参数放到intent中