Issue with Long click using instrumentation class in uiautomator for 4.3 android version

假如想象 提交于 2019-12-13 01:56:32

问题


I tried to performing long click on screen using the Motion Event class and instrumentation class.

This class seems to work fine for 4.2 and below devices but when I tried to implement the same on the 4.3 devices I'm facing the following exception

Could any one please help me out in resolving this problem

Thanks in Advance

The method which I used to perform long click event is

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)

回答1:


After lots of research I solved the above problem by using following piece of code

private void longClickOnScreen(int x, int y) {
        try {
            long downTime = SystemClock.uptimeMillis();
            long eventTime = SystemClock.uptimeMillis();
            final MotionEvent eventDown = MotionEvent.obtain(downTime,
                    eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
            eventDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
            final MotionEvent eventUp = MotionEvent.obtain(downTime, eventTime,
                    MotionEvent.ACTION_UP, x, y, 0);
            eventUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
            Thread task = new Thread(new Runnable() {

                private Handler handler;

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Looper.prepare();
                    handler = new Handler();
                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            inst.sendPointerSync(eventDown);
                            try {
                                Thread.sleep(750);
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            inst.sendPointerSync(eventUp);
                        }
                    });
                    Looper.loop();
                }
            });
            task.start();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }


来源:https://stackoverflow.com/questions/19156959/issue-with-long-click-using-instrumentation-class-in-uiautomator-for-4-3-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!