1

I want to make an Android TV app which behaves like traditional TV, when the up and down button on a remote control is pressed, TV channel is switched. I have mapped the onKeyDown listener. However, everytime I press a button, "playback control glue" shows up. I need to press the button again to trigger the listen so as to switch channel.

Playback Control Glue: https://developer.android.com/training/tv/playback/transport-controls

Is there any way to disable the control glue" from showing up? Since my app plays live streams, I don't want user to pause the video, and seeking is not possible.

And also, how could the listen get the keydown event immediately without going through the "control glue"?

Thanks.

P.S. I am not familiar with Android development. I am modifying the TV sample code in Kotlin.

ロジャー
  • 347
  • 1
  • 6
  • 16

1 Answers1

0

If you want to handle the key events in the activity, and not go through the control glue, you can override dispatchKeyEvent in your activity.

Like this:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP 
            || event.geyKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
        //switch channel
        return true;
    }
    return super.dispatchKeyEvent(event);
}
Breiby
  • 554
  • 5
  • 14
  • Although it shows an error in AndroidStudio, it works. Thanks. https://stackoverflow.com/questions/41150995/appcompatactivity-oncreate-can-only-be-called-from-within-the-same-library-group – ロジャー Jul 22 '19 at 08:21
  • I have found each key-press triggers my function twice. Does a KeyEvent include key-up and key-down? https://github.com/y2kbug-hk/dev.thematrix.tvhk/blob/master/app/src/main/java/dev/thematrix/tvhk/PlaybackActivity.kt – ロジャー Jul 22 '19 at 16:24
  • `dispatchKeyEvent` is triggered multiple times when you press a key. Once for the down press, then multiple times if you keep holding the button down, and then finally once when the button is released. You can check which event is being dispatched like this `event.getAction() ==` `KeyEvent.ACTION_DOWN` or `KeyEvent.ACTION_UP`. I recommend reacting on ACTION_UP, but if you want to do it on ACTION_DOWN you can check whether it is the initial press like this `event.getRepeatCount() == 0`. – Breiby Jul 23 '19 at 07:20