Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 301
JDS Avatar asked Jun 29 '11 02:06

JDS


People also ask

What are the types of broadcast receivers in Android?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.

What is the time limit of broadcast receiver in Android?

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.

What is the difference between static broadcast receivers and dynamic broadcast receivers?

The main difference in working between the static and dynamic receivers is that the static receivers will run if the application is running/not running. But the dynamic receivers will run if the application is running.


1 Answers

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.

like image 134
advantej Avatar answered Oct 02 '22 18:10

advantej