Android TV How To Prevent Other Applications In The Background From Using Media Buttons

359 Views Asked by At

On my Android TV if I launch Pandora in the background and let music play, then launch my app. From my application if I press pause or next it receives these on my applicaiotn but also affects Pandora.

How can I prevent background applications from receiving these inputs. I tried obtaining audio focus which effectively stops the music but if button input is made again music will resume.

Any ideas?

Here is my code capturing remote inputs.

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                    moveToNextImage(true);
                    break;
                case KeyEvent.KEYCODE_MEDIA_REWIND:
                    moveToPreviousImage(true);
                    break;
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    playPauseController();
                    break;
                default:
                    break;
            }
        }
        return super.onKeyDown(keyCode, event);
1

There are 1 best solutions below

0
On

Found it! To prevent other applications from receiving remote inputs while my app is active, add the handled boolean. Other option was gaining audio focus, but since I was not using audio it was not needed.

public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean handled = false;
        if ((event.getSource() & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
                    moveToNextImage(true);
                    handled = true;
                    break;
                case KeyEvent.KEYCODE_MEDIA_REWIND:
                    moveToPreviousImage(true);
                    handled = true;
                    break;
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    playPauseController();
                    handled = true;
                    break;
                default:
                    break;
            }
        }
        return handled || super.onKeyDown(keyCode, event);