views:

49

answers:

1

I'm currently exploring Broadcast Receivers and Broadcast Intent. There are some aspects of the tutorials which are ambiguous to me. To my knowledge this is how the process works. I can broadcast an intent from my application and any receiver on the phone can act on that intent as long as the actions match. I can register a broadcaster receiver on anything that extends Context and when I do so I must provide a filter which will allow the receiver to act on one or more intent actions.

The part which confuses me is defining a receiver via the manifest. Must the name of said receiver match a class name? Why should I ever define an intent filter with an action in the project manifest if I always have to provide another intent filter when I register my receiver in code?

If anyone has a good tutorial or example of the best way to use broadcast receivers and how I should define them I would appreciate it.

+2  A: 

I cant broadcast an intent from my application and any receiver on the phone can act on that intent as long as the actions match.

Yes, you can.

Must the name of said receiver match a class name?

Yes, because that is the class that implements the BroadcastReceiver.

Why should I ever define an intent filter with an action if whenever I register a receiver I have to provide an intent filter?

That sentence makes no sense.

Here are some examples of projects using manifest-registered BroadcastReceivers whose <intent-filter> filters on an action.

CommonsWare
@CommonsWare Let me rephrase that last question. Why should I ever define an intent filter with an action in the project manifest if I always have to provide another intent filter when I register my receiver in code? I'll look through the examples you provided. Thanks for the feed back.
Dave.B
"Why should I ever define an intent filter with an action in the project manifest if I always have to provide another intent filter when I register my receiver in code?" A `BroadcastReceiver` can be registered *either* through the manifest or through Java code. There is no "another" -- any given `BroadcastReceiver` will use one of the two techniques, not both. You use a manifest-registered receiver in cases where it is required by the SDK (e.g., for `BOOT_COMPLETED`) or where you need to respond to `Intents` without any other component running (e.g., for a scheduled alarm).
CommonsWare
So you're saying that if I register a receiver via the manifest it will load and run onReceive regardless of my applications state? I don't have to register it with a service or activity?
Dave.B
From looking at your example I see that my previous comment is true. Thank you for teaching me how powerful a tool receivers are and for providing clarity on the topic.
Dave.B
Note that there are some broadcasts that are not supported with manifest-registered receivers. `ACTION_BATTERY_CHANGED` is one. In those cases, they happen often enough that Android does not want to start up processes just to tell you that, say, the battery level changed.
CommonsWare