资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Monkey源码分析番外篇之Android注入事件的三种方法比较

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

我们提供的服务有:做网站、成都网站建设、微信公众号开发、网站优化、网站认证、玉树ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的玉树网站制作公司

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


Method 1: Using internal APIs

方法1:使用内部APIs
This 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

这种方法能在你的应用中很好的工作,但,也仅仅只能在你的应用中而已
Monkey源码分析番外篇之Android注入事件的三种方法比较

adb shell su chmod 666 /dev/input/event3 
You will need root to run the chmod command.

你将需要root权限来运行chmod命令。


 

作者

自主博客

微信

CSDN

天地会珠海分舵

http://techgogogo.com


服务号:TechGoGoGo

扫描码:

Monkey源码分析番外篇之Android注入事件的三种方法比较

http://cdkjz.cn/article/jgodsd.html

多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220