Search code examples
androidandroid-intentmediabroadcastreceiver

Receive long volume-key press


The Google-Music-App (on a OnePlus One, Cyanogen OS-Version 12.1) plays the next Song when the Volume-Up Hardware-Key is long-pressed and plays the previous Song on a Volume-Down Key long-press.

Is it possible to receive those actions in a BroadcastReceiver? Or does the OS just handle those long-clicks as a MEDIA_BUTTON Action? (If none of my two assumptions is right, how does Google Play do this?)

Edit 1:

Seems like it's a MEDIA_BUTTON Action with "MEDIA_NEXT". But why does my receiver not receive the action?

String action = intent.getAction();
    if(action.equalsIgnoreCase(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
                //my play code
            }else if (KeyEvent.KEYCODE_MEDIA_NEXT == event.getKeyCode()) {
                //my play next code
            }else if (KeyEvent.KEYCODE_MEDIA_PREVIOUS == event.getKeyCode()) {
            //my play prev code
        }else if (KeyEvent.KEYCODE_MEDIA_PAUSE == event.getKeyCode()) {
            //my pause code
        }
    }

it's registered in the manifest with:

<receiver android:name=".receiver.AudioPlayerBroadcastReceiver">
        <intent-filter>
            //other actions (they all work fine)
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

Solution

  • Cyanogen sends an MEDIA_NEXT event on long press of volume up. (at least Cyanogemod 11 did this on my phone)

    You should be able to test this easily. Just listen to MEDIA_NEXT and check if you receive it on long press.

    To edit 1: See BroadcastReceiver for ACTION_MEDIA_BUTTON not working

    (also: please open a new question instead of editing an old one if you have a new problem)