Android Send event 汇总 (点击,滑动,两指操作 源码)

UiAutomation 跨应用操作三大利器:

setOnAccessibilityEventListener() 开启Accessibility

executeShellCommand() 执行shell命令(权限比Runtime.getRuntime().exec()高,相当于adb shell)

injectInputEvent() 注入事件,比如点击。


EventCode
public static final int ACTION_DOWN             = 0;
public static final int ACTION_UP               = 1;
public static final int ACTION_MOVE             = 2;

先看看send event的方法:

Send Event

1.Instrumentation

Instrumentation inst = new Instrumentation();
nst.sendPointerSync

2.Uiautomation

mUiAutomation.injectInputEvent

3.IWindowManager

IWindowManager.Stub.asInterface(ServiceManager.getService("window"));

4.InputManager

MotionEvent me = getEvent();
InputManager.getInstance().injectInputEvent(me, 1)

 5.IWindowManager 

IBinder wmbinder = ServiceManager.getService( "window" ); 
IWindowManager m_WndManager = IWindowManager.Stub.asInterface( wmbinder ); 
// key down 
m_WndManager.injectKeyEvent( new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A ),true ); 
// key up

m_WndManager.injectKeyEvent( new KeyEvent( KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A ),true );

6./dev/input/eventX  这个需要权限哦

 

Touch

MotionEvent event = MotionEvent.obtain(downTime, eventTime, 0, (float) x, (float) y, 0);  //dowm
inst.sendPointerSync(MotionEvent.obtain(300 + downTime, 300 + eventTime, 1, (float) x, (float) y, 0)); //up

Swipe

   xStep = (double)(upX - downX) / (double)swipeSteps;
        yStep = (double)(upY - downY) / (double)swipeSteps;
        ret = this.touchDown(downX, downY);
        if(drag) {
            SystemClock.sleep(this.mUiAutomatorBridge.getSystemLongPressTime());
        }

        for(int i = 1; i < swipeSteps; ++i) {
            ret &= this.touchMove(downX + (int)(xStep * (double)i), downY + (int)(yStep * (double)i));
            if(!ret) {
                break;
            }

            SystemClock.sleep(5L);
        }

        if(drag) {
            SystemClock.sleep(100L);
        }

        ret &= this.touchUp(upX, upY);
pinchOpen
  private static MotionEvent getMotionEvent(long downTime, long eventTime, int action, List<PointerProperties> properties, List<PointerCoords> coordinates) {
        PointerProperties[] props = (PointerProperties[])properties.toArray(new PointerProperties[properties.size()]);
        PointerCoords[] coords = (PointerCoords[])coordinates.toArray(new PointerCoords[coordinates.size()]);
        return MotionEvent.obtain(downTime, eventTime, action, props.length, props, coords, 0, 0, 1.0F, 1.0F, 0, 0, 4098, 0);
    }

 



转载于:https://www.cnblogs.com/season-xie/p/6345320.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值