同时检测两个硬件按钮的按下

同时检测两个硬件按钮的按下

本文介绍了同时检测两个硬件按钮的按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下两个硬件按钮(例如音量按钮和电源按钮)时添加侦听器.但是我知道您无法覆盖电源按钮.其实我想拍一下屏幕,所以我想出了这个解决方案.因此,如果您对此有任何建议,请分享.

I want to add listener when my two hardware button presses like volume button and power button. But i came to know that you can't override power button. Actually i want to take snaps of screen so i came up with this solution. So this is what i wanted to achieve if you have any suggestion about this please share it.

推荐答案

在您的活动中.尝试使用以下代码调高音量+电源键.您可以降低PRESS_INTERVAL的效果,就像同时按下两个按钮一样.希望对您有所帮助!

In your Activity. Try below code for Volume UP + Power key. You can reduce PRESS_INTERVAL to get effect like both buttons are pressed at the same time. Hope This Helps!

private static final int PRESS_INTERVAL = 700;
private long mUpKeyEventTime = 0;
public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_POWER == event.getKeyCode()) {
            if ((event.getEventTime() - mUpKeyEventTime) < PRESS_INTERVAL) {
                // This is to check if Volume UP key and Power key are pressed at the same time.
                // Do the Task. Here You can add logic to take screenshot
            }
            return true;
        } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(KeyEvent.KEYCODE_VOLUME_UP == keyCode){
            mUpKeyEventTime = event.getEventTime();
        }
        return super.onKeyUp(keyCode, event);
    }

这篇关于同时检测两个硬件按钮的按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:08