原文:http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/#.VEoIoIuUcaV
我们提供的服务有:做网站、成都网站建设、微信公众号开发、网站优化、网站认证、玉树ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的玉树网站制作公司
往下分析monkey事件注入源码之前先了解下在android系统下事件注入的方式,翻译一篇国外文章如下。
该方法和其他所有内部没有向外正式公布的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:
//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
这种方法能在你的应用中很好的工作,但,也仅仅只能在你的应用中而已
adb shell su chmod 666 /dev/input/event3You will need root to run the chmod command.
你将需要root权限来运行chmod命令。
作者 | 自主博客 | 微信 | CSDN |
天地会珠海分舵 | http://techgogogo.com | 服务号:TechGoGoGo 扫描码: | http://jkwzsj.com/article/jgodsd.html |