Android以编程方式触发长按HOME键

Android以编程方式触发长按HOME键

本文介绍了Android以编程方式触发长按HOME键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种以编程方式模仿长按HOME按钮的操作的方法.我有使用以下代码的返回"按钮:

I am trying to figure out a way to programmatically mimic the action of a long HOME button press. I have BACK button working with following code:

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_BACK));

但是当我尝试模仿(长)HOME时,请按相同的方式:

But when I try to mimic a (long) HOME press same way:

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME));
// Thread.sleep(1000); Perhaps for long press?
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HOME));

什么都没发生.还有其他方法可以模仿HOME按钮的按下吗?

Nothing happens. Is there any other way to mimic the HOME button press?

推荐答案

您可以尝试以下操作:

this.dispatchKeyEvent(new KeyEvent((long) ViewConfiguration.getLongPressTimeout(), (long) 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME), 0); // ViewConfiguration.getLongPressTimeout() returns the duration in milliseconds before a press turns into a long press

它使用以下构造函数:

/**
 * Create a new key event.
 *
 * @param downTime The time (in {@link android.os.SystemClock#uptimeMillis})
 * at which this key code originally went down.
 * @param eventTime The time (in {@link android.os.SystemClock#uptimeMillis})
 * at which this event happened.
 * @param action Action code: either {@link #ACTION_DOWN},
 * {@link #ACTION_UP}, or {@link #ACTION_MULTIPLE}.
 * @param code The key code.
 * @param repeat A repeat count for down events (> 0 if this is after the
 * initial down) or event count for multiple events.
 */
public KeyEvent(long downTime, long eventTime, int action,
                int code, int repeat) {
    mDownTime = downTime;
    mEventTime = eventTime;
    mAction = action;
    mKeyCode = code;
    mRepeatCount = repeat;
    mDeviceId = KeyCharacterMap.VIRTUAL_KEYBOARD;
}

请参见.可能有帮助.

这篇关于Android以编程方式触发长按HOME键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:14