在分析monkey源码的时候有些背景知识没有搞清楚,比如在看到monkey是使用windowmanager的injectKeyEvent方法注入事件的时候,心里就打了个疙瘩,这种方式不是只能在当前应用中注入事件吗?Google了下发现了国外一个大牛有留下蛛丝马迹描述这个问题,特意摘录下来并做相应部分的翻译,其他部分大家喜欢就看下,我就不翻译了。

How it works

Behind the scenes, Monkey uses several private interfaces to communicate with three essential system services:

  1. Package Manager: Monkey uses the package manager to get a list of Activities for a given Intent. This enables Monkey to randomly switch between Activities while testing an application.
  2. Activity Manager: Monkey calls the very powerful setActivityController function on the Activity Manager. This effectively gives Monkey complete control over the activity life-cycle for the duration of the test.
  3. Window Manager: Monkey calls a series of functions on the Window Manager to inject events into the application. This enables Monkey to simulate touches and key-presses. Because Monkey communicates at this level, there is no obvious difference between events which have arrived from Monkey and events which have arrived from an actual user. In fact, the distinction is so seamless that it is sometimes necessary to manually check who is in control — hence the famous isUserAMonkey() method in the Android
Window Manager: Monkey通过调用WindowManager的一系列方法来注入事件到应用中。这样monkey可以模拟触摸和按键等用户行为。正是因为monkey是在这个层面和应用交互的,所以你的应用接收到的事件哪个是来自真实用户,哪个是来自monkey模拟的已经没有很明显的界限了。事实上正是因为这种近似无缝的区别,我们有时不得不去判断究竟是谁在控制着我们的设备了--这就是为什么android系统提供的isUserAMonkey()方法变得这么流行的原因了。

Monkey sends random events to any application you choose. In order to ensure that this doesn’t cause a security hole, Android uses several techniques to ensure that only monkey can send events, and only when the phone’s user is asking it to.

Monkey随机的往不同的的app发送随机事件。为了防止这种行为导致android自家的安全漏洞出来,android使用了几个技术来保证只有monkey可以,且在该手机设备用户允许的情况下才可以,往不同的app发送事件。

Firstly, Monkey itself can only be run by root, or by someone in the “shell” Unix group. Normally, only “adb shell” runs as the “shell group”. This means that the only way to run monkey is to do so through “adb shell”.

首先,monkey本身只能一是被root运行,二是被属于shell这个组的成员运行。而正常来说,只有”adb shell“是在shell这个组下运行的。这就意味着运行monkey的唯一方法就是通过‘adb shell’了。

Secondly, the Monkey application, which is mostly written in Java, asks for two special manifest permissions. The first, SET_ACTIVITY_WATCHER, allows Monkey to take control of the activity life-cycle. The second, INJECT_EVENTS, allows Monkey to simulate touches and key presses. Importantly, no normal Android application can request these permissions — they are only granted to applications supplied with the Android system. So there is little danger of a rogue APK taking control of an Android device using Monkey.

其次,monkey这个android自身提供的应用,大部分是用android的native语言java来编写的,它会向系统请求两个特背的manifest权限。第一个就是SET_ACTIVITY_WATCHER这个权限,它允许monkey对activity的生命周期进行全权控制。第二个就是INJECT_EVENTS这个权限它允许monkey去模拟触摸和按键事件。重要的是,正常的安卓app是不能请求到这些权限的--只有android系统同意的应用才会得到允许获得这些权限(译者注:其实就是需要android系统的AOSP系统签名。monkey是android自己维护编写的工具,当然是允许了) 以下是本人摘录的INJECT_EVENTS这个manifest选项的官方解析:

INJECT_EVENTS:Allows an application to inject user events (keys, touch, trackball) into the event stream and deliver them to ANY window. Monkey events What is an event? In Android, events are sent in  response to user input, or due to system events, such as power management. Monkey supports quite a few event types, but only three of them are of interest for automated testing:

  • KeyEvent: these events are sent by the window manager in response to hardware button presses, and also presses on the keyboard — whether hardware, or on-screen.
  • MotionEvent: sent by the window manager in response to presses on the touchscreen.
  • FlipEvent: sent when the user flips out the hardware keyboard on the HTC Dream. On that device, this would imply an orientation change. Unfortunately, Monkey does not simulate orientation changes on other devices.

作/译者

微信知识共享公众号

CSDN

天地会珠海分舵

TechGoGoGo

http://blog.csdn.net/zhubaitian

转载于:https://www.cnblogs.com/techgogogo/p/4284821.html

安卓WindowManager注入事件如何跳出进程间安全限制相关推荐

  1. python 进程间同步_python之路29 -- 多进程与进程同步(进程锁、信号量、事件)与进程间的通讯(队列和管道、生产者与消费者模型)与进程池...

    所谓异步是不需要等待被依赖的任务完成,只是通知被依赖的任务要完成什么工作,依赖的任务也立即执行,只要自己完成了整个任务就算完成了.至于被依赖的任务最终是否真正完成,依赖它的任务无法确定,所以它是不可靠 ...

  2. (28)System Verilog进程间同步(事件event)

    (28)System Verilog进程间同步(事件event) 1.1 目录 1)目录 2)FPGA简介 3)System Verilog简介 4)System Verilog进程间同步(事件eve ...

  3. 第十章 进程间的通信 之 Java/Android多线程开发(二)

    文章目录 (一)Java 多线程开发 1.1)线程状态 1.2)线程控制方法 (1.2.1)Synchronized (1.2.2)Volatile (1.2.3)ReentrantLock 1.3) ...

  4. linux进程间通讯-信号

    文章目录 进程间通信功能 信号 信号的概念 产生信号的方式 信号的默认(缺省)处理方式 进程接收到信号后的处理方式 kill函数 alarm函数 raise函数 abort函数 pause函数 sig ...

  5. python全栈开发,Day40(进程间通信(队列和管道),进程间的数据共享Manager,进程池Pool)...

    昨日内容回顾 进程 multiprocess Process -- 进程 在python中创建一个进程的模块startdaemon 守护进程join 等待子进程执行结束锁 Lock acquire r ...

  6. 同步线程和进程间的通信

    最近回去学习了一下进程和进程间的通信,有时候很多东西久不看了也就一下子忘了== 这里面有好几个互斥对象使用线程的 1 void mListText(CString str) 2 { 3 m_list_ ...

  7. Android-Binder进程间通讯机制-多图详解

    本系列: Android-Binder进程间通讯机制-多图详解 一次Binder通信最大可以传输多大的数据?​​​​​​​ 关于Binder (AIDL)的 oneway 机制 概述 最近在学习Bin ...

  8. 进程间基于消息队列的通信_Linux 进程间的通信方式

    (一)进程的概念 进程是操作系统的概念,每当我们执行一个程序时,对于操作系统来讲就是创建一个进程,在这个 过程中伴随着资源的分配和释放,可以认为进程是一个程序的一次执行过程. (二)进程间通信的概念 ...

  9. Linux进程间通讯

    最初Unix IPC包括:管道.FIFO.信号: System V IPC包括:System V消息队列.System V信号灯.System V共享内存区: Posix IPC包括: Posix消息 ...

  10. Storm通信机制,Worker进程间通信,Worker进程间通信分析,Worker进程间技术(Netty、ZeroMQ),Worker 内部通信技术(Disruptor)(来自学习资料)

    Storm通信机制 Worker间的通信经常需要通过网络跨节点进行,Storm使用ZeroMQ或Netty(0.9以后默认使用)作为进程间通信的消息框架. Worker进程内部通信:不同worker的 ...

最新文章

  1. 线程基本编程——线程函数大全
  2. J2EE中一些常用的名词【简】
  3. cmd不能用的解决方法
  4. What is AJAX?(转)(二)
  5. LeetCode 1863. 找出所有子集的异或总和再求和(DFS)
  6. python数据的格式输出_Python格式化输出
  7. yum安装好的php路径,yum安装php的路径是什么_后端开发
  8. 如何在win10系统上安装linux子系统
  9. gcc -l:手动添加链接库
  10. 算法:把排好序的链表转换为二叉排序树Convert Sorted List to Binary Search Tree
  11. 数学建模论文写作方法大总结
  12. 鼠标点计算机再点网络也是选定,鼠标左键单击变双击,点一下变两下是什么原因?...
  13. java多个点求连线_实现简单的粒子连线
  14. C# 汉字转拼音(支持GB2312字符集中所有汉字)
  15. 东北大学大物实验思考题答案解析
  16. linux 内核网络中 RPS/RFS 原理Ⅰ
  17. CSU 1725 加尔鲁什·地狱咆哮对阵虚灵大盗拉法姆
  18. 【转】中国只有俩导演,一个叫贾樟柯,一个叫姜文
  19. elementUI中el-table树形与el-tree树形结构的一键折叠与展开
  20. Navicat的安装及免费使用方式

热门文章

  1. python报告,python测试结果报告
  2. 前端----HTML
  3. 数据结构和算法——线性结构(1)数组、栈、队列和单链表
  4. Bellman_Ford算法(负环的单源路径)
  5. vscode还用装git_在windows下搭建编程环境git+vscode安装配置教程
  6. Collections 常用方法
  7. Oracle 临时表、数据闪回、系统常用表、及常用操作
  8. Alibaba 表格开源工具 easyexcel 快速使用教程
  9. LayaAir Sprite 旋转缩放
  10. 小D课堂 - 新版本微服务springcloud+Docker教程_5-07 断路器dashboard监控仪表