I have an IntentService which need to pass a message to an Activity.
I know two ways of doing so.
use
sendBroadcast()at theServiceside while registering abroadcastRecieverat theActivityside which will receiver the message.passing a
Messengerto the Serviceside, which will point to aHandlerat theActivityside, which will be ready to receive that message from the service.
Which one is good for which purpose? Or both of them do the same?
If your IntentService does not know whether the activity will exist (e.g., might have been destroyed), or if there are multiple activities that might be in the foreground and would want to know about what's going on, I'd use sendOrderedBroadcast(). You can arrange to then also have a "backstop" BroadcastReceiver that could raise a Notification, if desired, as I outline in this blog post and demonstrate in this sample project.
Either of your techniques can work, though.
来源:https://stackoverflow.com/questions/7372676/broadcastreceiver-or-messenger-via-handler