简体   繁体   中英

using remote control for android TV application

I'm developing an Android application for TV when I'm trying to test it in the real Android TV I can't navigate to any button or anything using the remote control buttons up, down, left, right.

I have searched about it and I found that I have to use (Enable D-pad Navigation ) but I don't know how to use this , I didn't find any code or tutorial for it My application is so simple. It makes dynamic layout beside each other and each layout has only one button and one recycle view.

By default Android implements basic D-Pad navigation inferred from the layout distribution. It works quite well if you use mostly lists or linear layouts.

The key difference is that the views are on the focused state instead of selected, so if you are using custom background ie for buttons it may look like it is not working while in fact it is.

You can try it out on an emulator using the cursor keys on the keyboard.

Alternatively or if the inferred navigation does not work you can define the nextFocusUp, Down, Left and Right per view.

It is all quite well explained on the official documentation: https://developer.android.com/training/tv/start/navigation.html

try this,

@Override
    public boolean onKeyDown(int keyCode, KeyEvent events) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_CENTER:

                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:

                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:

                break;
            case KeyEvent.KEYCODE_DPAD_UP:

                break;
            case KeyEvent.FLAG_KEEP_TOUCH_MODE:

                break;
        }
        return super.onKeyDown(keyCode, events);
    }

if in case call is not getInside above ,then try this:

  @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DPAD_CENTER:

                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:

                break;
            case KeyEvent.KEYCODE_DPAD_UP:

                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:

                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                break;
            case KeyEvent.FLAG_KEEP_TOUCH_MODE:

                break;
        }
        return super.dispatchKeyEvent(event);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM