Android receiver for multiple actions?

前端 未结 4 2081
我在风中等你
我在风中等你 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 <action>, <data>, or <category>. 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

    0 讨论(0)
  • 2021-02-19 11:16

    Yes, you can have multiple actions for the same BroadcastReceiver. In fact, what you did is just correct:

    <receiver android:name=".myReceiver">
         <intent-filter android:priority="1000000">
             <action android:name="android.intent.action.ACTION_HEADSET_PLUG"/>
             <action android:name="android.intent.action.MEDIA_BUTTON" />
         </intent-filter>
    </receiver>
    

    I have already used this approach in a project and it worked.

    0 讨论(0)
  • 2021-02-19 11:16

    about the priority you set:

    https://developer.android.com/guide/topics/manifest/intent-filter-element.html

    android:priority The priority that should be given to the parent component with regard to handling intents of the type described by the filter. This attribute has meaning for both activities and broadcast receivers:

    It provides information about how able an activity is to respond to an intent that matches the filter, relative to other activities that could also respond to the intent. When an intent could be handled by multiple activities with different priorities, Android will consider only those with higher priority values as potential targets for the intent.

    It controls the order in which broadcast receivers are executed to receive broadcast messages. Those with higher priority values are called before those with lower values. (The order applies only to synchronous messages; it's ignored for asynchronous messages.)

    Use this attribute only if you really need to impose a specific order in which the broadcasts are received, or want to force Android to prefer one activity over others.

    The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.

    0 讨论(0)
  • 2021-02-19 11:17

    I guess you can have multiple s each one having its action element.

    <receiver android:name=".myReceiver">
         <intent-filter android:priority="1000000">
             <action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
         </intent-filter>
    
         <intent-filter android:priority="1000000">
             <action android:name="android.intent.action.MEDIA_BUTTON" />
         </intent-filter>
    </receiver>
    

    And then check the Intent's action in the onReceive of the receiver.

    0 讨论(0)
提交回复
热议问题