Monkey源代码分析番外篇之Android注入事件的三种方法比較
原文:http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.VEoIoIuUcaV
往下分析monkey事件注入源代码之前先了解下在android系统下事件注入的方式。翻译一篇国外文章例如以下。
Method 1: Using internal APIs
该方法和其它全部内部没有向外正式发布的APIs一样存在它自己的风险。
The idea is to get an instance of WindowManager in order to access the injectKeyEvent / injectPointerEvent methods.
原理是通过获得WindowManager的一个实例来訪问injectKeyEvent/injectPointerEvent这两个事件注入方法。
IBinder wmbinder = ServiceManager.getService( "window" );
IWindowManager m_WndManager = IWindowManager.Stub.asInterface( wmbinder );
The ServiceManager and WindowsManager are defined as Stubs. We can then bind to these services and call the methods we need.
ServiceManager和Windowsmanager被定义为存根Stubs类。我们依据我们的须要绑定上这些服务并訪问里面的方法。
To send a key do the following:
通过下面方式发送一个事件:
// 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 );
To send touch/mouse events use:
发送touch/mouse事件:
//pozx goes from 0 to SCREEN WIDTH , pozy goes from 0 to SCREEN HEIGHT
m_WndManager.injectPointerEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,pozx, pozy, 0), true);
m_WndManager.injectPointerEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,pozx, pozy, 0), true);
This works fine, but only inside your application
这样的方法能在你的应用中非常好的工作,但,也只只能在你的应用中而已
The moment you're trying to inject keys/touch events to any other window, you'll get a force close because of the following exception:
一旦你想要往其它窗体注入keys/touch事件,你将会得到一个强制关闭的消息:
E/AndroidRuntime(4908): java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission
Not much joy, as INJECT_EVENTS is a system permission. A possible solution is discussed here and here.
苦逼了吧,毕竟INJECT_EVENTS是须要系统权限的,一些可能解决的方案在这里和这里有讨论到。
(译者注:请查看本人上一篇翻译的《Monkey源代码分析番外篇之WindowManager注入事件怎样跳出进程间安全限制》里面有更具体针对这个问题的描写叙述)
Method 2: Using an instrumentation object
This is a clean solution based on public API, but unfortunately it still requires that INJECT_EVENTS permission.
相对以上的隐藏接口和方法,这个是比較干净(上面的是隐藏的。故须要用到android不干净不推荐的方法去获取)的方式,但不幸的事它依旧有上面的JINECT_EVENTS这个仅仅有系统应用(基本上就是android自己提供的,如monkey)才被同意的权限问题。
Instrumentation m_Instrumentation = new Instrumentation();
m_Instrumentation.sendKeyDownUpSync( KeyEvent.KEYCODE_B );
For touch events you can use:
下面是触摸事件实例:
//pozx goes from 0 to SCREEN WIDTH , pozy goes from 0 to SCREEN HEIGHT
m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,pozx, pozy, 0);
m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,pozx, pozy, 0);
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1YmFpdGlhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
All good inside the test application, and will crash instantly when trying to inject keys to outside apps, not because the approach doesn't work, but because Android Developers have chosen so. Thanks guys, you rock! Not.
在应用内操作的话全然没有问题,但一旦跳出这个应用去触发按键事件的话就会崩溃。不是由于这种方法不工作,而是由于android开发人员做了限制。谢谢你们,android的开发人员们,你牛逼!
个屁。
By looking at sendPointerSync's code, you will quickly see it uses the same approach as presented in method 1). So this is the same thing, but packed nicely in a easy to use API:
通过分析sendPointerSync的相应代码。能够看到事实上instrumentation使用到的注入事件方式事实上和方法一提到的通过WindowManager.injectPointerEvents是一样的。所以穿的都是同一条内裤,仅仅是Robotium出来走动的时候套上条时尚喇叭裤。而以上直接调用WindowManager的方式就宛如仅仅穿一条内裤出街的差别而已。
public void sendPointerSync(MotionEvent event) {
validateNotAppThread();
try {
(IWindowManager.Stub.asInterface(ServiceManager.getService("window")))
.injectPointerEvent(event, true);
} catch (RemoteException e) {
}
}
Method 3: Direct event injection to /dev/input/eventX
Linux exposes a uniform input event interface for each device as /dev/input/eventX where X is an integer. We can use it directly and skip the above Android Platform permission issues.
linux以系统设备的方式向用户暴露了一套统一的事件注入接口/dev/input/eventX(当中X代表一个整数)。
我们能够直接跳用而跳过以上的平台(android这个机遇linux的平台)限制问题。
For this to work, we will need root access, so this approach only works on a rooted device.
可是这须要工作的话,你须要rooted过的设备。
By default the eventX files have the permission set for 660 (read and write for Owner and Group only). To inject keys from our application, we need to make it writable. So do this first:
设备文件eventX默认是被设置为660这个权限的(Owner和同组成员有读写,而owner是root)。为了向这个设备注入事件。你必须让它能可写。所以请先做下面动作:
adb shell
su
chmod 666 /dev/input/event3
You will need root to run the chmod command.
你将须要root权限来执行chmod命令。
| 
 作者  | 
 自主博客  | 
 微信  | 
 CSDN  | 
| 
 天地会珠海分舵  | 
 服务号:TechGoGoGo 扫描码:  | 
 http://blog.csdn.net/zhubaitian  | 
Monkey源代码分析番外篇之Android注入事件的三种方法比較的更多相关文章
- Android注入事件的三种方法比较
		
方法1:使用内部APIs 该方法和其他所有内部没有向外正式公布的APIs一样存在它自己的风险.原理是通过获得WindowManager的一个实例来访问injectKeyEvent/injectPoin ...
 - Monkey源代码分析番外篇WindowManager如何出的喷射事件的进程间的安全限制
		
在分析monkey源代码时的一些背景知识不明确,例如看到monkey它是用windowmanager的injectKeyEvent的喷射事件时的方法.我发现自己陷入疙瘩,这种方法不仅能够在当前的应用程 ...
 - monkey源代码分析之事件注入方法变化
		
在上一篇文章<Monkey源代码分析之事件注入>中.我们看到了monkey在注入事件的时候用到了<Monkey源代码分析番外篇之Android注入事件的三种方法比較>中的第一种 ...
 - Monkey源代码分析之事件注入
		
本系列的上一篇文章<Monkey源代码分析之事件源>中我们描写叙述了monkey是怎么从事件源取得命令.然后将命令转换成事件放到事件队列里面的.可是到如今位置我们还没有了解monkey里面 ...
 - Second Day: 关于Button监听事件的三种方法(匿名类、外部类、继承接口)
		
第一种:通过匿名类实现对Button事件的监听 首先在XML文件中拖入一个Button按钮,并设好ID,其次在主文件.java中进行控件初始化(Private声明),随后通过SetOnClickLis ...
 - python学习番外篇——字符串的数据类型转换及内置方法
		
目录 字符串的数据类型转换及内置方法 类型转换 内置方法 优先掌握的方法 需要掌握的方法 strip, lstrip, rstrip lower, upper, islower, isupper 插入 ...
 - 快速定位 Android APP 当前页面的三种方法(Activity / Fragment)
		
方法一.通过adb命令打印当前页面: Android 如何快速定位当前页面是哪个Activity or Fragment (1)查看当前Activity :adb shell "dumpsy ...
 - android 获取sharedpreference的三种方法的区别
		
1. public SharedPreferences getPreferences (int mode) 通过Activity对象获取,获取的是本Activity私有的Preference,保存在系 ...
 - android 设置颜色的三种方法
		
1.利于系统自带的颜色类 如TextView1.setTextColor(Android.graphics.Color.RED); 2.数字颜色表示法 TextView1.setTextColor(0 ...
 
随机推荐
- 本地文件夹变远程仓库并且提交Github
			
//初始化本地仓库 $ git init Initialized empty Git repository in C:/Users/root/Desktop/vue-music/.git/ root@ ...
 - kafka delete topic
			
Don't think it is supported yet. Take a look at this JIRA tracking Delete Topic. For delete manually ...
 - 【React Native开发】React Native控件之ListView组件解说以及最齐全实例(19)
			
),React Native技术交流4群(458982758).请不要反复加群!欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章 ...
 - TP5报错variable type error: array
			
variable type error: array 当你在tp5框架中写方法时返回一个数组时,tp5会报错:variable type error: array 这是因为tp5不支持返回数组. ...
 - JVM:垃圾回收机制和调优手段
			
转载请注明出处: jiq•钦's technical Blog - 季义钦 引言: 我们都知道JVM内存由几个部分组成:堆.方法区.栈.程序计数器.本地方法栈 JVM垃圾回收仅针对公共内存区域即:堆和 ...
 - C10K——千万级并发实现的秘密:内核不是解决方案,而是问题所在!(转)
			
既然我们已经解决了 C10K并发连接问题,应该如何提高水平支持千万级并发连接?你可能会说不可能.不,现在系统已经在用你可能不熟悉甚至激进的方式支持千万级别的并发连接. 要知道它是如何做到的,我们首先要 ...
 - maven Dynamic Web Module 3.0 requires Java 1.6 or newer
			
maven Dynamic Web Module 3.0 requires Java 1.6 or newer CreateTime--2018年4月19日16:56:42 Author:Mary ...
 - [TODO]com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method
			
异常信息如下: 2018-10-30 20:00:50.230 ERROR java.util.concurrent.ExecutionException: com.alibaba.dubbo.rpc ...
 - 使用JDK自带的Stax操作XML
			
操作的books.xml <?xml version="1.0" encoding="UTF-8"?> <bookstore> < ...
 - Oracle快速克隆安装
			
Oracle的家目录进行快速克隆,对同类型机器配置很高效的! 01.确认你在克隆数据库时,原数据库已经关闭 sqlplus / as sysdba; shutdown immediate ; ...