开发者

How to report from a BroadcastReceiver back to a Service in Android?

I have a service that is running and that is interested in being notified if packages are installed, removed, or updated.

I understand that I should use a Broadcast receiver, but it clearly says in BroadcastReceiver that...

you may not show a dialog or bind to a service from within a BroadcastReceiver

So my question is - How can I inform the service that such bro开发者_JS百科adcasts have been made.


You can't bind to it but nothing prohibits to call the service via a an intent :

context.startService( <your intent with a name that match the filter of your service> );

Then put your data within extras of this intent, as usual.

Another option could be AIDL but I didn't explore that yet.


To expand on the text you quoted...

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

From the Context.startService() docs...

If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.

In other words, a win/win situation as calling startService() on a service which is already running won't have an adverse effect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜