开发者

BroadcastReceiver peekService() returning null

开发者 https://www.devze.com 2023-02-16 22:30 出处:网络
I\'m trying to get a IBinder from a Service so I can communicate with the service when receiving a notification from the system. From my onReceive() method I\'m calling peekService() but I\'m getting

I'm trying to get a IBinder from a Service so I can communicate with the service when receiving a notification from the system. From my onReceive() method I'm calling peekService() but I'm getting a null reference.

I've read some answers here https://groups.google.com/forum/#!topic/android-developers/yiq_XIzFoS4 from Dianne and Mark where开发者_C百科 they recommend using startService but my service has an interface and I think peekService should work, but it doesn't.

Do I need permissions to peek a service from a BroadcastReceiver? Do I need something else? I'm sure the service is running but I can't get a binder from it.


If the communication is BroadcastReceiver -> Service... then you would use startService(), passing data by adding it to the Intent's Bundle.

I suspect your BroadcastReceiver needs to ask the Service (or its IBinder) a question. In this case you use peekService. The rub with peekService is that it returns null if the service is not started and been bound to.

The answer is to ensure the service is started and has been bound to, before your broadcast is received. How you accomplish that will vary and depend on your specific application.

If the Service you are peeking is part of your application (same APK) then you do not need special permissions.

0

精彩评论

暂无评论...
验证码 换一张
取 消