广播接收器peekService()返回null接收器、peekService、null

2023-09-06 05:34:12 作者:长发贞子重口味/短发素颜小清新

我试图让的IBinder 服务这样我就可以与服务通信接收时从系统通知。从我的的onReceive()方法我打电话 peekService(),但我得到一个空引用。

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.

我读过一些答案在这里 https://groups.google .COM /论坛/#!话题/ Android的开发者/ yiq_XIzFoS4从戴安娜和马克他们推荐使用 startService ,但我的服务有一个接口,而且我认为 peekService 应该工作,但它没有。

I've read some answers here https://groups.google.com/forum/#!topic/android-developers/yiq_XIzFoS4 from Dianne and Mark where 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.

推荐答案

如果通信是广播接收器 - >服务......那么你会被它添加到意图的捆绑使用startService(),传递数据

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

我怀疑你的广播接收器需要询问服务(或其的IBinder)的问题。在这种情况下,你使用peekService。与peekService擦的是,它如果服务没有启动,并绑定到返回null。

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.

如果你正在偷看服务为您的应用程序(APK一样),那么你并不需要特殊权限的一部分。

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