我尝试使用 Motion Event 类和检测类在屏幕上执行长按。
此类似乎适用于 4.2 及以下设备,但是当我尝试在 4.3 设备上实现相同功能时,我面临以下异常
谁能帮我解决这个问题
提前致谢
我用来执行长点击事件的方法是
private void longClickOnScreen(float x, float y) {
try {
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis();
MotionEvent event = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_DOWN, x, y, 0);
MotionEvent event2 = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, x, y, 0);
inst.sendPointerSync(event);
Thread.sleep(750);
inst.sendPointerSync(event2);
} catch (NullPointerException e) {
// TODO: handle exception
e.printStackTrace();
}
}
java.lang.RuntimeException: This method can not be called from the main application thread
at android.app.Instrumentation.validateNotAppThread(Instrumentation.java:1651)
at android.app.Instrumentation.sendPointerSync(Instrumentation.java:933)
at com.kpt.adaptxt.uitest.event.EventHandler.longClickOnScreen(EventHandler.java:84)
at com.kpt.adaptxt.uitest.event.EventHandler.longClick(EventHandler.java:149)
at com.kpt.adaptxt.uitest.testsuite.UiautomationTestsuite.testLongpressEvent(UiautomationTes
tsuite.java:24)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.start(UiAutomatorTestRunner.java
:160)
at com.android.uiautomator.testrunner.UiAutomatorTestRunner.run(UiAutomatorTestRunner.java:9
6)
at com.android.commands.uiautomator.RunTestCommand.run(RunTestCommand.java:91)
at com.android.commands.uiautomator.Launcher.main(Launcher.java:83)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:235)
at dalvik.system.NativeStart.main(Native Method)