【问题标题】:Android programmatically trigger long HOME pressAndroid以编程方式触发长按HOME
【发布时间】:2019-12-29 12:19:14
【问题描述】:

我正在尝试找出一种以编程方式模拟长按 HOME 按钮的动作的方法。我有使用以下代码的 BACK 按钮:

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

但是当我尝试以同样的方式模仿(长)HOME 键时:

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 键?

【问题讨论】:

  • 据我所知,我问这个,我可以知道按住长按主页按钮的主要目的是什么吗?
  • 其实我不知道该怎么做,所以我无法回答你的问题。但请注意,当您长按主页按钮时,并非所有 Android 手机都有相同的行为。一些打开应用程序切换器,另一些打开助手。另外我想知道您是否可以忽略按钮的默认行为。去看看这个问题
  • 好吧,在新的 Amazon Fire 设备上,长按 Home 按钮会触发 Alexa 进行监听,这就是我想要以编程方式实现的目标。

标签: android keyevent


【解决方案1】:

你可以试试这个:

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;
}

this。这可能会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多