BroadcastReceiver for ACTION_MEDIA_BUTTON not working
I tested this on a Samsung Galaxy S5 with Android 4.4.2. So what is important and what is not mentioned in other posts:
- Register the receiver in the AndroidManifest.xml inside the application tag but outside from every activity tag.
- Your receiver Broadcastreceiver need to be public and static
- One activity need to register an MediaButtonEventReceiver to listen for the button presses
Okay and here some example code:
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mReceiverComponent = new ComponentName(this,YourBroadcastReceiver.class);
...
mAudioManager.registerMediaButtonEventReceiver(mReceiverComponent);
...
// somewhere else
mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent);
Here the receiver:
public static class YourBroadcastReceiver extends BroadcastReceiver{
// Constructor is mandatory
public MediaBroadcastReceiver ()
{
super ();
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
Log.i (TAG_MEDIA, intentAction.toString() + " happended");
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
Log.i (TAG_MEDIA, "no media button information");
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
Log.i (TAG_MEDIA, "no keypress");
return;
}
// other stuff you want to do
}
}
And here the manifest snippet. If needed add priority for the intent-filter, but was not needed for me:
<application>
<receiver android:name="OuterClass$YourBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
<activity> ... </activity>
</application>
For the references:
- http://developer.android.com/training/managing-audio/volume-playback.html
- MediaButtonIntentReceiver not working in Android 4.0+
- BroadcastReceiver: can't instantiate class; no empty constructor
broadcastreceiver onReceive problem ACTION_MEDIA_BUTTON Android
First, you should stop using Toast
as your diagnostic test. Use the Log
class to log to LogCat, or breakpoints, or something.
Next, you should get rid of your MediaButtonIntentReceiver
constructor, as it is not needed.
Then, I would dump the if (!Intent.ACTION_MEDIA_BUTTON.equals(v_IntentAction))
block, since it is also not needed.
Next, I would make sure that your media button actually works. Does it control other applications, like a music player? It may be that your media button is not Android-compliant or something.
Why does ACTION_MEDIA_BUTTON fail to process events?
Do you happen to use Library Project to implement this?
If so, which manifest did you put the <receiver>
in? The library's or the application's?
If you put in the library's, it won't work. You must place this in the application's manifest:
<receiver android:name=".RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Related Topics
What Is the Use of Memoryfile in Android
Android Java.Net.Unknownhostexception: Host Is Unresolved (Strategy Question)
How to Highlight Multiple Words in Edittext
Read Data from SQLite Where Column Name Contains Spaces
Android JSON Parsing of Multiple JSONobjects Inside JSONobject
Android - Viewrootimpl$Calledfromwrongthreadexception
Read/Write .Txt File with Special Characters
What Is the Correct Way to Do Inserts/Updates/Deletes in Android SQLitedatabase Using a Query String
Android:Table Has No Column Named "Variable Name" MySQL Database Error
Google Cloud Messaging: Don't Receive Alerts When iOS App Is in Background
Java Apns Certificate Error with "Derinputstream.Getlength(): Lengthtag=109, Too Big."
Can't Install Rjava on Ubuntu System
Unable to Compile Jni Program Rjava
Simple Program to Call R from Java Using Eclipse and Rserve
How Does One Configure Rjava on Osx to Select the Right Jvm -- .Jinit() Failing