gpt4 book ai didi

java - Android UIAutomator 长按设备

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:27:15 24 4
gpt4 key购买 nike

我想在指定时间长按指定点。不幸的是,类中没有像长按这样的方法:UiDevice我可能可以编写自己的方法,如下所示:

private void longClick(int x, int y, long time) {
android.graphics.Point point = new android.graphics.Point(x, y);
android.graphics.Point[] points = new android.graphics.Point[2];
points[0] = point;
points[1] = point;
getUiDevice().swipe(points, time / 5); // according to documentation, each step lasts 5ms
}

或者使用反射并调用 longTap:

private void longClick(int x, int y) {
Field mUiAutomationBridgeField = getUiDevice().getClass().getDeclaredField("mUiAutomationBridge");
mUiAutomationBridgeField.setAccessible(true);
Object mUiAutomationBridge = mUiAutomationBridgeField.get(getUiDevice());
Field mInteractionControllerField = mUiAutomationBridge.getClass().getDeclaredField("mInteractionController");
mInteractionControllerField.setAccessible(true);
Object mInteractionController = mInteractionControllerField.get(mUiAutomationBridge);
Method longTap = mInteractionController.getClass().getDeclaredMethod("longTap", int.class, int.class);
longTap.setAccessible(true);
longTap.invoke(mInteractionController, x, y);
}

但是它不是令人满意的解决方案,知道如何做得更好吗?为什么他们会错过这种方法?

最佳答案

getUiDevice().getInstance().swipe(x, y, x, y, 400);

起点和终点相同。然后就可以模拟uiDevice长按了。

关于java - Android UIAutomator 长按设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16061478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com