Skip to content Skip to sidebar Skip to footer

Issue With Long Click Using Instrumentation Class In Uiautomator For 4.3 Android Version

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 impl

Solution 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();
        }
    }

Post a Comment for "Issue With Long Click Using Instrumentation Class In Uiautomator For 4.3 Android Version"