Reputation: 374
I'm trying to capture the ACTION_MEDIA_BUTTON intent using a broadcastreceiver in Android 3.0+.
My receiver is a static inner class of my MainActivity class. It is static, because it is registered in my AndroidManifest.xml and it has to find the class. However, this means that my BroadcastReceiver has no way of getting back to my activity when the play/pause button is pressed. The onReceive method gets called, but because the class is static, I can't inform my activity.
Using a reference to my activity or a Handler object also does not work, since I can't acquire the BroadcastReceiver object that is being called by the Android system.
Dynamically declaring the receiver should also work, but this does not work on Android 3.0+, for some strange reason. It has something to to with:
AudioManager.registerMediaButtonEventReceiver(ComponentName)
Which is required to be called.
Some illustration of my class:
public class MainActivity extends Activity {
public static class MicReceiver extends BroadcastReceiver {
// onReceive is called
// How do I inform MainActivity of the press?
}
}
Do you have any ideas for a fix?
Thanks!
[EDIT] See my code below for registering my receiver dynamically: (This is currently not working)
mReceiver = new RemoteControlReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
filter.setPriority(2147483647);
registerReceiver(mReceiver, filter);
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName()));
Upvotes: 1
Views: 881
Reputation: 401
The code should look like this if you want to process something in BroadcastReceiver
public class MainActivity extends Activity {
public static class MicReceiver extends BroadcastReceiver {
public MicReceiver() {
// TODO Auto-generated constructor stub
super();
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
Toast.makeText(context, "BUTTON PRESSED! ", Toast.LENGTH_SHORT).show();
}
abortBroadcast();
}
}
}
Upvotes: 0
Reputation: 1006869
AFAIK, registerMediaButtonEventReceiver()
is if you want to receive media button events in the background. A foreground activity can find out about media button events using the standard onKeyDown()
callback.
Upvotes: 2
Reputation: 29436
Declaring receiver in Manifest will try to instantiate the receiver and call onRecieve()
, even when activity is not around.
To make it an activity bound receiver, make it a non static class and instantiate it in onCreate()
. Then, register and unregister it in onResume()
and onPause()
respectively. Since class is non static and registered only when activity is active, you can safely call parent activity methods from inner receiver class.
Upvotes: 0