如何从 InstrumentationTestCase 发送长按?

发布于 2024-09-03 01:34:18 字数 125 浏览 4 评论 0原文

在 Android 中,如何从 InstrumentationTestCase 发送长按?例如,我想做一个 sendKeys(KEYCODE_DPAD_CENTER) 但要长时间点击。

In Android, how can I send a long press from an InstrumentationTestCase? I'd like for instance to do a sendKeys(KEYCODE_DPAD_CENTER) but make that a long click.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雅心素梦 2024-09-10 01:34:18

不知道这是否是唯一/正确的方法,但我设法这样做:

public void longClickDpadCenter() throws Exception {
    getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
    Thread.sleep(ViewConfiguration.get(mContext).getLongPressTimeout());
    getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
}

Don't know if this is the only/proper way, but I managed to do it this way:

public void longClickDpadCenter() throws Exception {
    getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
    Thread.sleep(ViewConfiguration.get(mContext).getLongPressTimeout());
    getInstrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
}
十六岁半 2024-09-10 01:34:18

您可以尝试下面的帮助方法:

private void longPress(int key) {
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    KeyEvent event1 = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN, key, 0);
    KeyEvent event2 = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN, key, 1);
    getInstrumentation().sendKeySync(event1);
    getInstrumentation().sendKeySync(event2);
}

以及使用示例:

longPress(KeyEvent.KEYCODE_ENTER);

You could try the helper method below:

private void longPress(int key) {
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();
    KeyEvent event1 = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN, key, 0);
    KeyEvent event2 = new KeyEvent(downTime, eventTime, KeyEvent.ACTION_DOWN, key, 1);
    getInstrumentation().sendKeySync(event1);
    getInstrumentation().sendKeySync(event2);
}

And example of usage:

longPress(KeyEvent.KEYCODE_ENTER);
绅刃 2024-09-10 01:34:18

android 源代码:

class InputKeyEvent implements InputCmd {
    @Override
    public void run(int inputSource, int displayId) {
        String arg = nextArgRequired();
        final boolean longpress = "--longpress".equals(arg);
        if (longpress) {
            arg = nextArgRequired();
        }

        do {
            final int keycode = KeyEvent.keyCodeFromString(arg);
            sendKeyEvent(inputSource, keycode, longpress, displayId);
        } while ((arg = nextArg()) != null);
    }

    private void sendKeyEvent(int inputSource, int keyCode, boolean longpress, int displayId) {
        final long now = SystemClock.uptimeMillis();
        int repeatCount = 0;

        KeyEvent event = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, repeatCount,
                0 /*metaState*/, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /*scancode*/, 0 /*flags*/,
                inputSource);
        event.setDisplayId(displayId);

        injectKeyEvent(event);
        if (longpress) {
            repeatCount++;
            injectKeyEvent(KeyEvent.changeTimeRepeat(event, now, repeatCount,
                    KeyEvent.FLAG_LONG_PRESS));
        }
        injectKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
    }
}

Line 179 in the android source code:

class InputKeyEvent implements InputCmd {
    @Override
    public void run(int inputSource, int displayId) {
        String arg = nextArgRequired();
        final boolean longpress = "--longpress".equals(arg);
        if (longpress) {
            arg = nextArgRequired();
        }

        do {
            final int keycode = KeyEvent.keyCodeFromString(arg);
            sendKeyEvent(inputSource, keycode, longpress, displayId);
        } while ((arg = nextArg()) != null);
    }

    private void sendKeyEvent(int inputSource, int keyCode, boolean longpress, int displayId) {
        final long now = SystemClock.uptimeMillis();
        int repeatCount = 0;

        KeyEvent event = new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyCode, repeatCount,
                0 /*metaState*/, KeyCharacterMap.VIRTUAL_KEYBOARD, 0 /*scancode*/, 0 /*flags*/,
                inputSource);
        event.setDisplayId(displayId);

        injectKeyEvent(event);
        if (longpress) {
            repeatCount++;
            injectKeyEvent(KeyEvent.changeTimeRepeat(event, now, repeatCount,
                    KeyEvent.FLAG_LONG_PRESS));
        }
        injectKeyEvent(KeyEvent.changeAction(event, KeyEvent.ACTION_UP));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文