首页 技术 正文
技术 2022年11月24日
0 收藏 428 点赞 3,318 浏览 4499 个字

原文:http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.VEoIoIuUcaV

往下分析monkey事件注入源代码之前先了解下在android系统下事件注入的方式。翻译一篇国外文章例如以下。

Method 1: Using internal APIs

方法1:使用内部APIsThis approach has its risks, like it is always with internal, unpublished 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

方法2: 使用instrumentation对象

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

方法3:直接注入事件到设备/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

天地会珠海分舵

http://techgogogo.com

服务号:TechGoGoGo

扫描码:

http://blog.csdn.net/zhubaitian

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731