Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android UIAutomator long click on device

I would like to perform long click on specified point with specified time. Unfortunately there is no method like long click in class: UiDevice I probably could write own method, something like this:

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
}

or use reflection and invoke 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);
}

However it's not satisfied solution, any idea how to do it better way? Why do they miss such method?

like image 352
maszter Avatar asked Apr 17 '13 13:04

maszter


2 Answers

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

Start point and end point are same. Then you can simulate uiDevice longclick.

like image 196
YanWang Avatar answered Sep 30 '22 12:09

YanWang


I think swipe a UiObject is better.

yourUiObject.swipeRight(int steps);
yourUiObject.swipeLeft(int steps);
yourUiObject.swipeDown(int steps);
yourUiObject.swipeUp(int steps);

Anyone of them works great for me. Notice that the doc says one step takes about 5ms, but I found it's not true.

like image 34
Jaybo Avatar answered Sep 30 '22 12:09

Jaybo