1. 使用Instrumentation

new Thread(new Runnable() {            @Override            public void run() {                Instrumentation mInst = new Instrumentation();                mInst.sendKeyDownUpSync(KeyEvent.KEYCODE_A); //按键事件                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMilis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, point.x, point.y, 0); //触摸按下                mInst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMilis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, point.x, point.y, 0); //触摸抬起                }        }).start();

查看sendPointerSync()方法的源码会看到,它使用的是 injectInputEvent()方法实现:

        public void sendPointerSync(MotionEvent event) {            validateNotAppThread();            if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) == 0) {                event.setSource(InputDevice.SOURCE_TOUCHSCREEN);            }            InputManager.getInstance().injectInputEvent(event,                    InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);        }   

使用Instrumentation需要权限,并且跨App注入事件需要系统应用(AndroidManifest.xml 中加入android:sharedUserId="android.uid.system")并且平台签名才行。


2. 使用反射直接调用injectInputEvent() 方法:

    private void invokeInjectInputEvent(MotionEvent event) {        Class cl = InputManager.class;        try {            Method method = cl.getMethod("getInstance");            Object result = method.invoke(cl);            InputManager im = (InputManager) result;            method = cl.getMethod("injectInputEvent", InputEvent.class, int.class);            method.invoke(im, event, 0);        } catch (IllegalAccessException e) {            e.printStackTrace();        }  catch (IllegalArgumentException e) {           e.printStackTrace();        } catch (NoSuchMethodException e) {            e.printStackTrace();        }catch (InvocationTargetException e) {            e.printStackTrace();        }    }

3.使用Runtime.exec 执行

    Process process = Runtime.getRuntime().exec("su");    DataOutputStream os = new DataOutputStream(process.getOutputStream());    String cmd = "/system/bin/input tap point.x point.y\n";    os.writeBytes(cmd);    os.writeBytes("exit\n");    os.flush();    os.close();    process.waitFor();

或者直接:

    Runtime.getRuntime().exec(new String[] {"su", "-c", "input tap " + point.x + " " + point.y});

4. 针对特定的应用可以使用Android Accessibility


5. 可以直接使用Adb shell

adb shell input tap point.x point.y

6.monkeyrunner是一个功能更为丰富的选项,不过需要连接电脑

参考链接:
1. http://azard.me/blog/2015/06/13/android-cross-app-touch-event-injection/
2. http://blog.csdn.net/mad1989/article/details/38109689
3. http://www.race604.com/android-inject-input-event/

更多相关文章

  1. Android(安卓)查看内存使用情况
  2. Android(安卓)Service Bind启动调用service方法
  3. taintDroid系统性能测试之——Android(安卓)AVD命令行使用
  4. Android(安卓)退出程序方式——再按一次退出
  5. 浅谈Java中Collections.sort对List排序的两种方法
  6. 箭头函数的基础使用
  7. NPM 和webpack 的基础使用
  8. Python list sort方法的具体使用
  9. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程

随机推荐

  1. DLC
  2. 备份实战案例1
  3. 备份实战案例2
  4. shell编程及自动化运维----变量
  5. 1. 实例演示Ajax的get,post请求; 2. 练习
  6. 其他空间类的引用,别名引用,自动加载类,数据
  7. 210402 JavaScript 作用域 闭包 原型 构
  8. Vue:实例演示,v-if,v-for,v-model,v-bind
  9. 优盘文件或目录损坏且无法读取怎么办 ,文
  10. 快递100支持快递公司编码案例