Search code examples
androidandroid-intentbroadcastreceiver

Android receiver for multiple actions?


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

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

So in myReceiver class' onReceive, could I check intent.getAction() for both ACTION_MEDIA_BUTTON and ACTION_HEADSET_PLUG?


Solution

  • 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.