2

I've followed instructions on this page until "Preparing your code for Android 2.2 without restricting it to Android 2.2". Even if I'm building against 2.3 version, I guess it should be working at this point but it's not. I've registered a receiver in manifest:

<receiver android:name="RemoteControlReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

created a class for RemoteControlReceiver declaration:

public class RemoteControlReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show();
    }
}

and finally boostrapped it in the starting activity.

private AudioManager _audioManager;
private ComponentName _componentName;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    _audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    _componentName = new ComponentName(getPackageName(), RemoteControlReceiver.class.getName());
}

@Override
protected void onResume() {
    super.onResume();

    _audioManager.registerMediaButtonEventReceiver(_componentName);

    _audioManager.requestAudioFocus(new OnAudioFocusChangeListener() {

            @Override
            public void onAudioFocusChange(int focusChange) {
                    Toast.makeText(getApplicationContext(), "onFocusChanged", Toast.LENGTH_SHORT).show();
            }
    }, AudioManager.STREAM_MUSIC, 0);
}

Could you point out what am I missing here ? I have an assumption that in order to receive thodse messages I my activity has to play any media stuff. I gonna test it right away.

P.S. As you see I've even added some uneccessary code - don't pay attention to requestAudioFocus.

Thanks for any suggestions.

user1462299
  • 4,247
  • 5
  • 22
  • 30
  • As my experiences, it seems listening to volume buttons by broadcast receivers, or outside of the app is impossible. Here is [my question](http://stackoverflow.com/questions/10154118/listen-to-volume-buttons-in-background-service) (which has no answer till now). –  Jul 07 '12 at 08:21
  • But I've seen apps that do respond to physical volume button clicks. Those apps somehow are able to adjust their elements like volume seekbars when a user hits those buttons. – user1462299 Jul 07 '12 at 08:25
  • 1
    If your app is running in foreground, you can use [onKeyDown()](http://developer.android.com/reference/android/app/Activity.html#onKeyDown(int,%20android.view.KeyEvent))/ [onKeyUp()](http://developer.android.com/reference/android/app/Activity.html#onKeyUp(int,%20android.view.KeyEvent))… I've kept searching for other solutions but no luck :-( –  Jul 07 '12 at 08:32

0 Answers0