开发者

How to communicate back to a service from a broadcast receiver?

开发者 https://www.devze.com 2023-01-11 19:23 出处:网络
Hope someone can help me out here.I will try to be concise! I have a widget which starts a service. The service registers two broadcast receivers.I would like to send back intents from the receivers

Hope someone can help me out here. I will try to be concise!

I have a widget which starts a service. The service registers two broadcast receivers. I would like to send back intents from the receivers to the service, so that the service can react.

I believe I read somewhere that 'starting' the serv开发者_Python百科ice multiple times works, e.g. do the following in the receivers:

serviceIntent.setAction("me.SERVICE");
intent.putExtra("me.SERVICE", somedata);
context.startService(serviceIntent);

I remember reading (on some blog) that this won't start a new service, but will simply pass the intent to the already running service. Is this correct? Is it a bad way of doing it? Is there a better way?

Thanks very much!

Jack


Yes, I've used that approach in a pre-2.0 app.


I accomplished this using a singleton. I set a private variable (pointing to 'this') in the service's onCreate, and then used a static method called getInstance() which would return it. So later on, I simply call MyService.getInstance() to get hold of the service.

0

精彩评论

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