Android Instrumentation 模拟点击、拖拽、发送keycode、焦点异常丢失。

Instrumentation模拟点击,拖拽,向系统发送keycode。

需要注意的几点:

  1. Android系统坐标系,X轴正方向为右,Y轴正方向为下(与数学坐标系相反)
  2. 所有事件在发送时均需要在Thread中执行,否则会报异常。
  3. 模拟输入法种特殊符号时,需要带上shift,同正常键盘操作。
public void simulateClick() {

        final float clickX = mPositionX + mHalfScreenWidth;
        final float clickY = mPositionY + mHalfScreenHeight;

        long downTime = SystemClock.uptimeMillis();
        inst.sendPointerSync(MotionEvent.obtain(downTime, downTime + 100, MotionEvent.ACTION_DOWN, clickX, clickY, 0));
        inst.sendPointerSync(MotionEvent.obtain(downTime, downTime + 100, MotionEvent.ACTION_UP, clickX, clickY, 0));

    }
 public void simulateTouch(int event, float x, float y) {

        mPositionX += x;
        mPositionY += y;


        if (mPositionX > mHalfScreenWidth) {
            mPositionX = mHalfScreenWidth;
        } else if (mPositionX < (-mHalfScreenWidth)) {
            mPositionX = -mHalfScreenWidth;
        }

        if (mPositionY > mHalfScreenHeight) {
            mPositionY = mHalfScreenHeight;
        } else if (mPositionY < (-mHalfScreenHeight)) {
            mPositionY = -mHalfScreenHeight;
        }


        float moveX = mPositionX + mHalfScreenWidth;
        float moveY = mPositionY + mHalfScreenHeight;

        long downTime = SystemClock.uptimeMillis();
        MotionEvent motionEvent = MotionEvent.obtain(downTime, downTime + 100, event, moveX, moveY, 0);

        try {
            inst.sendPointerSync(motionEvent);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "Exception = " + e);
        }

    }
private static boolean simulateKeystroke(int KeyCode) {

        try {
            Instrumentation inst = new Instrumentation();

            if (shift) {
                inst.sendKeySync(new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyCode, 0, KeyEvent.META_SHIFT_ON));
                inst.sendKeySync(new KeyEvent(0, 0, KeyEvent.ACTION_UP, KeyCode, 0, KeyEvent.META_SHIFT_ON));
                //  shift = false;
            } else {
                inst.sendKeyDownUpSync(KeyCode);
            }

            return true;
        } catch (Exception e) {
            // TODO: handle exception
            return false;
        }
    }

Android 设备启动后,会默认为Touch模式。如果你做过Android TV开发,发现开机后launcher的焦点丢失,或者有异常情况。请参考如下代码。

   Instrumentation instrumentation = new Instrumentation();
        instrumentation.setInTouchMode(false);

Android Instrumentation源码分析(附Activity启动流程)

移动测试基础 Android Instrumentation 框架简单说明

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值