我想在指定时间长按指定点。不幸的是,在类中没有像 long click 这样的方法: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);
}
但是它不满意的解决方案,知道如何做得更好吗?他们为什么会错过这种方法?