简体   繁体   English

安卓 从遥控器遥控Android电视盒

[英]Android. Remote the android tv box from a remote control

You have propaply seen those pretty cheap android tv boxes available on the market. 您已经恰当地看到了市场上那些非常便宜的android电视盒。 They are usually followed with a remote control that has some functionalities like clicking, swipe or slide to left and right up and down. 通常,它们后面是带有一些功能的遥控器,例如单击,滑动或上下左右滑动。

Recently i made an app and tried to navigate it using the remote control. 最近,我制作了一个应用程序,并尝试使用遥控器进行导航。 I have some gesture methods in the project. 我在项目中有一些手势方法。 I tried to swipe to left and right but the app didnt do anything while when i try it on my phone that has a screen gets the gesture and do what it should. 我尝试左右滑动,但是当我在有屏幕的手机上尝试该应用程序时,该应用程序没有执行任何操作,并且可以执行手势操作。 Like opening the navigation drawer etc. 像打开导航抽屉等。

Now to my question: does one need to use speciel methods? 现在我的问题是:是否需要使用特殊方法? Is there some rules that one should be ware of? 有一些应注意的规则吗?

Edit This is what Ive done so far. 编辑这是我到目前为止所做的。 I made a class that defines the action: It's from google.developer 我制作了一个定义操作的类:来自google.developer

    public class Dpad {
        public final static int UP = 0;
        public final static int LEFT = 1;
        public final static int RIGHT = 2;
        public final static int DOWN = 3;
        public final static int CENTER = 4;

        int directionPressed = -1; // initialized to -1

        public int getDirectionPressed(InputEvent event) {
            if (!isDpadDevice(event)) {
                return -1;
            }

            // If the input event is a MotionEvent, check its hat axis values.
            if (event instanceof MotionEvent) {

                // Use the hat axis value to find the D-pad direction
                MotionEvent motionEvent = (MotionEvent) event;
                float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
                float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);

                // Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
                // LEFT and RIGHT direction accordingly.
                if (Float.compare(xaxis, -1.0f) == 0) {
                    directionPressed = Dpad.LEFT;
                } else if (Float.compare(xaxis, 1.0f) == 0) {
                    directionPressed = Dpad.RIGHT;
                }
                // Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
                // UP and DOWN direction accordingly.
                else if (Float.compare(yaxis, -1.0f) == 0) {
                    directionPressed = Dpad.UP;
                } else if (Float.compare(yaxis, 1.0f) == 0) {
                    directionPressed = Dpad.DOWN;
                }
            }

            // If the input event is a KeyEvent, check its key code.
            else if (event instanceof KeyEvent) {

                // Use the key code to find the D-pad direction.
                KeyEvent keyEvent = (KeyEvent) event;
                if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
                    directionPressed = Dpad.LEFT;
                } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
                    directionPressed = Dpad.RIGHT;
                } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
                    directionPressed = Dpad.UP;
                } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
                    directionPressed = Dpad.DOWN;
                } else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
                    directionPressed = Dpad.CENTER;
                }
            }
            return directionPressed;
        }

        public static boolean isDpadDevice(InputEvent event) {
            // Check that input comes from a device with directional pads.
            if ((event.getSource() & InputDevice.SOURCE_DPAD)
                    != InputDevice.SOURCE_DPAD) {
                return true;
            } else {
                return false;
            }
        }
    }

In my MainActivity I have a navigationdrawer. 在我的MainActivity中,我有一个导航抽屉。 which I want to open and close when remotecontrol D-pad wants it 我想在遥控器D-pad想要它时打开和关闭它

 mDrawerLayout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
        @Override
        public boolean onGenericMotion(View view, MotionEvent motionEvent) {
            if (Dpad.isDpadDevice(motionEvent)) {
                int press = mDpad.getDirectionPressed(motionEvent);
                switch (press) {
                    case Dpad.RIGHT:
                        // Do something for UP direction press Open the drawer
                        mDrawerLayout.openDrawer(Gravity.START);
                        return true;
                    case Dpad.LEFT:

                        mDrawerLayout.closeDrawer(Gravity.START);
                        return true;

                }
            }
            return false;
        }
    });

In one of the fragments I have a media player and using D-pad up and D-pad down I change the video. 在其中一个片段中,我有一个媒体播放器,使用D-pad up和D-pad down我可以更改视频。

    v.setOnGenericMotionListener(new View.OnGenericMotionListener() {
        @Override
        public boolean onGenericMotion(View view, MotionEvent motionEvent) {
            if (Dpad.isDpadDevice(motionEvent)) {
                int press = mDpad.getDirectionPressed(motionEvent);
                switch (press) {
                    case Dpad.UP:
                        // Do something for UP direction press
                        UP(); // Change the video to next
                        return true;
                    case Dpad.DOWN:
                        DOWN(); // Change the video the earlier on
                        return true;

                }
            }

            return false;
        }
    });

EDIT It' now become a problem since it doesn't response to any motion. 编辑由于它不响应任何动作,因此现在成为问题。 I tried on a emulator with physical keyboard and not a single action happens. 我尝试使用带有物理键盘的仿真器,但没有一个动作发生。 I would appritiate if someone gives me a hint. 如果有人给我提示,我将不胜感激。 i mean this kind of remote control in the picture below 我的意思是下图中的这种遥控器 在此处输入图片说明

Thanks in advance 提前致谢

I think the code on Android Developer is wrong: 我认为Android Developer上的代码是错误的:

public static boolean isDpadDevice(InputEvent event) {
    // Check that input comes from a device with directional pads.
    if ((event.getSource() & InputDevice.SOURCE_DPAD)
         != InputDevice.SOURCE_DPAD) {
         return true;
     } else {
         return false;
     }
}

The condition should be "==" instead of "!=", so this is correct: 条件应该是“ ==”而不是“!=“,所以这是正确的:

    public static boolean isDpadDevice(InputEvent event) {
    // Check that input comes from a device with directional pads.
    if ((event.getSource() & InputDevice.SOURCE_DPAD)
         == InputDevice.SOURCE_DPAD) {
         return true;
     } else {
         return false;
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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