打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android代码模拟物理、屏幕点击事件 、APP内部自动点击

一、应用中模拟物理和屏幕点击事件

例如,模拟对某个view的点击事件

  1. private void simulateClick(View view, float x, float y) {
  2. long downTime = SystemClock.uptimeMillis();
  3. final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime,MotionEvent.ACTION_DOWN, x, y, 0);
  4. downTime += 1000;
  5. final MotionEvent upEvent = MotionEvent.obtain(downTime, downTime,MotionEvent.ACTION_UP, x, y, 0);
  6. view.onTouchEvent(downEvent);
  7. view.onTouchEvent(upEvent);
  8. downEvent.recycle();
  9. upEvent.recycle();
  10. }
  11. public void setMouseClick(int x, int y){
  12. MotionEvent evenDownt = MotionEvent.obtain(System.currentTimeMillis(),
  13. System.currentTimeMillis() + 100, MotionEvent.ACTION_DOWN, x, y, 0);
  14. dispatchTouchEvent(evenDownt);
  15. MotionEvent eventUp = MotionEvent.obtain(System.currentTimeMillis(),
  16. System.currentTimeMillis() + 100, MotionEvent.ACTION_UP, x, y, 0);
  17. dispatchTouchEvent(eventUp);
  18. evenDownt.recycle();
  19. eventUp.recycle();
  20. }

这实现原理就是模拟两个MotionEvent (按下和提起) 然后用一个View 来处理这个Event 。

二、Instrumentation实现模拟键盘鼠标事件

  1. // 可以不用在 Activity 中增加任何处理,各 Activity 都可以响应
  2. Instrumentation inst = new Instrumentation();
  3. inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),
  4. MotionEvent.ACTION_DOWN, 200, 500, 0));
  5. inst.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),
  6. MotionEvent.ACTION_UP, 200, 500, 0));

三、系统中模拟物理和屏幕点击事件

1、adb shell 进入手机命令行 
2、getevent -h 用法说明

  1. shell@hwH60:/ $ getevent -h
  2. Usage: getevent [-t] [-n] [-s switchmask] [-S] [-v [mask]] [-d] [-p] [-i] [-l] [-q] [-c count] [-r] [device]
  3. -t: show time stamps
  4. -n: don't print newlines
  5. -s: print switch states for given bits
  6. -S: print all switch states
  7. -v: verbosity mask (errs=1, dev=2, name=4, info=8, vers=16, pos. events=32, props=64)
  8. -d: show HID descriptor, if available
  9. -p: show possible events (errs, dev, name, pos. events)
  10. -i: show all device info and possible events
  11. -l: label event types and names in plain text
  12. -q: quiet (clear verbosity mask)
  13. -c: print given number of events then exit
  14. -r: print rate events are received

[-t] 参数显示事件的时间戳 
[-n] 取消事件显示时的换行符 
[-s switchmask] 得到指定位的开关状态 
[-S] 得到所有开关的状态 
[-v [mask]] 根据mask的值显示相关信息 
[-p] 显示每个设备支持的事件类型和编码 
[-q] 只显示事件数据 
[-c count] 只显示count次事件的数据 
[-r] 显示事件接收频率

3、getevent -p 显示出来当前系统存在的所有input设备,并且把每个设备支持的事件类型以及编码

  1. shell@hwH60:/ $ getevent -p
  2. add device 1: /dev/input/event2
  3. name: "hi6421_on"
  4. events:
  5. KEY (0001): 0074
  6. input props:
  7. <none>
  8. could not get driver version for /dev/input/mouse0, Not a typewriter
  9. add device 2: /dev/input/event4
  10. name: "huawei,touchscreen"
  11. events:
  12. KEY (0001): 003b 003c 003d 003e 003f 0040 0041 0042
  13. 0043 0044 0057 00bd 00be 00bf 00c0 00c1
  14. 0145 014a
  15. ABS (0003): 0000 : value 0, min 0, max 1079, fuzz 0, flat 0, resolution 0
  16. 0001 : value 0, min 0, max 1919, fuzz 0, flat 0, resolution 0
  17. 0018 : value 0, min 0, max 255, fuzz 0, flat 0, resolution 0
  18. 0030 : value 0, min 0, max 15, fuzz 0, flat 0, resolution 0
  19. 0035 : value 0, min 0, max 1079, fuzz 0, flat 0, resolution 0
  20. 0036 : value 0, min 0, max 1919, fuzz 0, flat 0, resolution 0
  21. 0039 : value 0, min 0, max 15, fuzz 0, flat 0, resolution 0
  22. 003a : value 0, min 0, max 255, fuzz 0, flat 0, resolution 0
  23. input props:
  24. INPUT_PROP_DIRECT
  25. add device 3: /dev/input/event0
  26. name: "mhl_rcp_dev"
  27. events:
  28. KEY (0001): 0002 0003 0004 0005 0006 0007 0008 0009
  29. 000a 000b 000e 001c 0034 003b 003c 003d
  30. 003e 003f 0067 0069 006a 006c 0071 0072
  31. 0073 0077 0080 008b 009e 009f 00a1 00a4
  32. 00a5 00a7 00a8 00ae 00c8 00c9 00cf 00d0
  33. 00d5 00e8 0161 0163 0192 0193 019c
  34. input props:
  35. <none>
  36. could not get driver version for /dev/input/mice, Not a typewriter
  37. add device 4: /dev/input/event1
  38. name: "hisi_gpio_key.14"
  39. events:
  40. KEY (0001): 0072 0073
  41. input props:
  42. <none>
  43. add device 5: /dev/input/event3
  44. name: "hi3630_hi6401_CARD Headset Jack"
  45. events:
  46. KEY (0001): 0072 0073 00e2
  47. SW (0005): 0002 0004
  48. input props:
  49. <none>

4、getevent 查看输入设备和查看事件 
打印输出log日志,等待输入设备,我们触摸屏幕或是手机物理按键,便会看到这里的变化

  1. shell@hwH60:/ $ getevent
  2. 例如:
  3. /dev/input/event0: 0001 014a 00000001
  4. /dev/input/event0: 0003 0000 000000f6
  5. /dev/input/event0: 0003 0001 000002ed
  6. /dev/input/event0: 0003 0035 000000f6
  7. /dev/input/event0: 0003 0036 000002ed
  8. /dev/input/event0: 0003 0032 00000001
  9. /dev/input/event0: 0003 0039 00000000
  10. /dev/input/event0: 0003 003a 00000043
  11. /dev/input/event0: 0000 0002 00000000

他们四个参数对应的是device type code value 
device:指的是处理触摸和按键的输入设备。 
type:指的是事件类型,EV_SYN [0000] (同步事件),EV_KEY [0001] (按键事件),EV_ABS [0003] (绝对值事件) 
code 指的是前面type代表的事件中支持的编码。 
value 指的是值。

例如:需要模拟一次点击BACK键,模拟点击的功能通常都是使用 /dev/input/event0 这个输入设备,back键的类型为 0001(按键事件),BACK的编码为 0x9e 转换为十进制后即158

注意的是在getevent中code显示的是十六进制,而sendevent时需要用十进制

那我们输入如下命令即可模拟一次BACK键的按下和弹起:

  1. adb shell sendevent /dev/input/event0 1 158 1
  2. adb shell sendevent /dev/input/event0 1 158 0

5、input keyevent 命令

先列举 input keyevent 几个比较常用的code值:

  1. input keyevent 3 // Home
  2. input keyevent 4 // Back
  3. input keyevent 19 //Up
  4. input keyevent 20 //Down
  5. input keyevent 21 //Left
  6. input keyevent 22 //Right
  7. input keyevent 23 //Select/Ok
  8. input keyevent 24 //Volume+
  9. input keyevent 25 // Volume-
  10. input keyevent 82 // Menu 菜单

例如: 
点击back键

shell@hwH60:/ $ input keyevent 3 

input text 命令 
输入框输入内容的。后面参数为 “字符串”,例如输入”helloworld”字符串

shell@hwH60:/ $ input text "helloworld!"

input tap 命令 
模拟单击事件 后面参数为: x y ,例如点击(168,252)位置

shell@hwH60:/ $ input tap 168 252  

input swipe 命令 
此命令为滑动事件。例如:从 30 10 滑动到 30 100

shell@hwH60:/ $ input swipe 30 10 30 100

Android代码实现,注意需要root

  1. private void execShellCmd(String cmd) {
  2. try {
  3. // 申请获取root权限,这一步很重要,不然会没有作用
  4. Process process = Runtime.getRuntime().exec("su");
  5. // 获取输出流
  6. OutputStream outputStream = process.getOutputStream();
  7. DataOutputStream dataOutputStream = new DataOutputStream(
  8. outputStream);
  9. dataOutputStream.writeBytes(cmd);
  10. dataOutputStream.flush();
  11. dataOutputStream.close();
  12. outputStream.close();
  13. } catch (Throwable t) {
  14. t.printStackTrace();
  15. }
  16. }
  1. execShellCmd("getevent -p");
  2. execShellCmd("sendevent /dev/input/event0 1 158 1");
  3. execShellCmd("sendevent /dev/input/event0 1 158 0");
  4. execShellCmd("input keyevent 3");//home
  5. execShellCmd("input text 'helloworld!' ");
  6. execShellCmd("input tap 168 252");
  7. execShellCmd("input swipe 100 250 200 280");
<uses-permission android:name = "android.permission.INJECT_EVENTS"/>

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
getevent/sendevent 使用说明
Android通过代码模拟物理、屏幕点击事件
Android getevent/sendevent用法详解
Android:adb中使用getevent/sendevent模拟touch操作
Android 2.3 input输入事件处理
Android模拟产生事件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服