I am writing an Android application for version 4.0.3 (ICS) of the Android OS. The issue is that I am not getting the output from my Log.d() in the onReceive() method of the BroadcastReceiver which means my application is not properly handling the broadcast.
I have read countless questions about how to run code upon a ACTION_MEDIA_BUTTON being clicked. I have even copy + pasted code when mine did not work, just to see if it would work.
The ACTION_MEDIA_BUTTON I want to handle is the single button on earphones that allow a user to pickup / end calls, play / pause music. Instead of my application handling this button, when I click it, the stock music player on my Nexus S Android starts playing a song.
I have not placed my code in another class, maybe this is why it's not working?
Here's the code found on the onCreate() method (this specific code I copied off a website after the code I wrote didn't work):
IntentFilter mediaButtonFilter = new IntentFilter(
Intent.ACTION_MEDIA_BUTTON);
mediaButtonFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
BroadcastReceiver brMediaButton = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d("Event", "Media button!");
this.abortBroadcast();
KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if(key.getAction() == KeyEvent.ACTION_UP) {
int keycode = key.getKeyCode();
if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT) {
Log.d("TestApp", "Next Pressed");
} else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
Log.d("TestApp", "Previous pressed");
} else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
Log.d("TestApp", "Head Set Hook pressed");
}
}
}
};
registerReceiver(brMediaButton, mediaButtonFilter);
All I really need to test for is the KEYCODE_HEADSETHOOK but it doesn't hurt to have the other code there for testing, I'll fix it up once I can get everything working correctly.
In my manifest:
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
I originally thought this may be a permissions issue since I didn't specify any permissions for this however I didn't receive any error message.
Like I said earlier, I have tried many variations of this. One example was the use of the code at this link broadcastreceiver onReceive problem ACTION_MEDIA_BUTTON Android with CommonsWare's corrections. Again, however, I modified it so it wasn't in a seperate class.
Thank you in advance for your help.