I am trying to receive the physical button events such as volume button clicks on the side of the phone. I need to do that in a Service because I want to receive if the user clicks on the Volume up/down button at any time. I read a lot in the net, but I cannot make it work and don't know what could be wrong. This is my code:
In the Manifest:
<receiver android:name=".clsMediaButtonReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
In the service OnStart():
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), clsMediaButtonReceiver.class.getName()));
An the broascastreceiver:
public class clsMediaButtonReceiver extends BroadcastReceiver
{
@Override public void onReceive(Context context, Intent intent)
{ //This is never shown
Toast toast1 = Toast.makeText(Context, intent.getAction(), Toast.LENGTH_SHORT);
toast1.show();
}
}