您的位置:首页 > 移动开发 > Android开发

【android】几种模拟按键、屏幕点击的方法

2016-12-22 15:29 501 查看
模拟按键:

1、 new Instrumentation().sendKeyDownUpSync(int keycode);  

       发送keycode,down,up都会发送一遍

2、 new Instrumentation().sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, int mKeycode) ;

       发送keycode,可以指定发送down,或者up

模拟屏幕点击

3、 new Instrumentation().sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y,    0);

4、ProcessBuilder pb = new ProcessBuilder("input tap " + x + " " + y);

      pb.directory(new File("/system/bin"));

      pb.start();

以上方法只能在当前的应用里面模拟按键和屏幕点击,如果要实现全局的模拟按键需要系统权限才能执行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息