Android receiver for multiple actions?

前端 未结 4 2009
我在风中等你
我在风中等你 2021-02-19 10:36

Simple question - Can I register a single BroadcastReceiver to multiple Intent actions? Here\'s what I\'m considering:



        
4条回答
  •  青春惊慌失措
    2021-02-19 11:14

    Yes, one BroadcastReceiver can receive intents with several actions. And your example is valid. You may have multiple "actions" in one filter. Moreover, in this case it is more appropriate than what seems a safer way: creating several filters with a single action (as proposed in the answer by @advantej). Having several actions in a filter clearly communicates that you are expecting one of several actions (logical "or").

    These words from "Intent and Intent filters" explain choice between one vs. many intent filters for more complex cases:

    It's okay to create a filter that includes more than one instance of , , or . If you do, you simply need to be certain that the component can handle any and all combinations of those filter elements.

    When you want to handle multiple kinds of intents, but only in specific combinations of action, data, and category type, then you need to create multiple intent filters.

    Please see different examples of Intent filters here: AndroidManifest.xml

提交回复
热议问题