?????????uiautomator???????????????У????????????????????uiautomator????????longClick()?????????????????????????????д?÷?????
??????????????£?
public static boolean longClickWithTime(UiObject obj?? long time) throws Exception {
Class<?> InteractionControllerClass = null;
Method findAccessibilityNodeInfoMethod = null;
Method getInteractionControllerMethod = null;
Method touchDownMethod = null;
Method touchUpMethod = null;
Field WAIT_FOR_SELECTOR_TIMEOUT = null;
findAccessibilityNodeInfoMethod = UiObject.class.getDeclaredMethod("findAccessibilityNodeInfo"??long.class);
findAccessibilityNodeInfoMethod.setAccessible(true);
WAIT_FOR_SELECTOR_TIMEOUT = UiObject.class.getDeclaredField("WAIT_FOR_SELECTOR_TIMEOUT");
WAIT_FOR_SELECTOR_TIMEOUT.setAccessible(true);
AccessibilityNodeInfo node = (AccessibilityNodeInfo) findAccessibilityNodeInfoMethod.invoke(obj?? WAIT_FOR_SELECTOR_TIMEOUT.getLong(obj));
if(node == null) {
throw new UiObjectNotFoundException(obj.getSelector().toString());
}
Rect rect = obj.getVisibleBounds();
getInteractionControllerMethod = UiObject.class.getDeclaredMethod("getInteractionController");
getInteractionControllerMethod.setAccessible(true);
Object interactionController = getInteractionControllerMethod.invoke(obj);
InteractionControllerClass = Class.forName("com.android.uiautomator.core.InteractionController");
touchDownMethod = InteractionControllerClass.getDeclaredMethod("touchDown"?? int.class?? int.class);
touchDownMethod.setAccessible(true);
touchUpMethod = InteractionControllerClass.getDeclaredMethod("touchUp"?? int.class?? int.class);
touchUpMethod.setAccessible(true);
if(Boolean.parseBoolean(touchDownMethod.invoke(interactionController?? rect.centerX()?? rect.centerY()).toString())) {
SystemClock.sleep(time);
if(Boolean.parseBoolean(touchUpMethod.invoke(interactionController?? rect.centerX()?? rect.centerY()).toString())) {
return true;
}
}
return false;
}
???????÷????
?????????uiautomator???????????????longClickWithTime(Object?? time)??????????UiObject???????Ч????????Object??????uiautomator UiObject???????time??????????????λ?????
???????磺????????????????г???5s??????
????longClickWithTime(new UiCollection(new UiObject(new UiSelector().text("Clock"))?? 5000)??