The successful Android platform has been around for a few years now. End users get lots of bells and whistles in fancy applications, but for developers, this "open-intended" platform seems even more tangled up than ever.
There are various APIs that still do not work, others that get deprecated-and-out in a single release, or some that are locked up for the supreme security purpose, invoked so very often by platform developers, that probably feel they've given the right answer.
All in one, Android also comes with a lot of frustration, because tools that are needed to accomplish various tasks or even more advanced applications, are put behind bars on purpose.

Another example of many is injecting a key press programmatically or the similar counter-part, injecting a touch event (mouse).
Because a malicious programmer might develop a software that would open your Market app and download payed apps without you knowing about it, developers world wide are prohibited from sending a key press programmatically to other applications except their own.

This is just ridiculous narrow thinking.

Of course there will always be a balance between security and functionality, but with paranoia it quickly turns to total failure. In other words why limit something so useful that can create so little damage? Users are warned about risks with normal permissions and it should stay that way for other features as well.

I'll just leave them with their issues and get back to the topic: I am aware of three methods for injecting events programmatically. This refers both to keyboard events (keys) and mouse events (touch events).

Method 1: Using internal APIs

This approach has its risks, like it is always with internal, unpublished APIs.
The idea is to get an instance of WindowManager in order to access the injectKeyEvent / injectPointerEvent methods.

          
  1. IBinder wmbinder = ServiceManager.getService( "window" );
  2. 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. The interfaces are included in the sample code attached at the end of this article.

To send a key do the following:

          
  1. // key down
  2. m_WndManager.injectKeyEvent( new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A ),true );
  3. // key up
  4. m_WndManager.injectKeyEvent( new KeyEvent( KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A ),true );

To send touch/mouse events use:

          
  1. //pozx goes from 0 to SCREEN WIDTH , pozy goes from 0 to SCREEN HEIGHT
  2. m_WndManager.injectPointerEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
  3. SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,pozx, pozy, 0), true);
  4. m_WndManager.injectPointerEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
  5. 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:



          
  1. 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.

Method 2: Using an instrumentation object

This is a clean solution based on public API, but unfortunately it still requires that INJECT_EVENTS permission.

          
  1. Instrumentation m_Instrumentation = new Instrumentation();
  2. m_Instrumentation.sendKeyDownUpSync( KeyEvent.KEYCODE_B );

For touch events you can use:

          
  1. //pozx goes from 0 to SCREEN WIDTH , pozy goes from 0 to SCREEN HEIGHT
  2. m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
  3. SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,pozx, pozy, 0);
  4. m_Instrumentation.sendPointerSync(MotionEvent.obtain(SystemClock.uptimeMillis(),
  5. SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,pozx, pozy, 0);


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.
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:

          
  1. public void sendPointerSync(MotionEvent event) {
  2. validateNotAppThread();
  3. try {
  4. (IWindowManager.Stub.asInterface(ServiceManager.getService("window")))
  5. .injectPointerEvent(event, true);
  6. } catch (RemoteException e) {
  7. }
  8. }

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.
For this to work, we will need root access, so this approach only works on a rooted device.
I felt it was easier to deal with linux using native C code, but a pure java implementation is also possible. Therefore I have added a small JNI component to handle the interface with /dev/input/eventX.

The sample code I wrote doesn't detect the X number automatically, so make sure you set that before running the code. By default I set it to event3. You can change that in NativeInput.java, see code below.

As I said, this last method requires root. 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:

          
  1. adb shell
  2. su
  3. chmod 666 /dev/input/event3

You will need root to run the chmod command.

Sample Code

AndroidKeyInjector

Other great resources:

Generating keypresses programmatically
Internal input event handling in the Linux kernel and the Android userspace
androidscreencast

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android webview开发html5
  2. Android之Activity透明背景
  3. Android Tips---Android平台常见属性集合
  4. 【Android常用控件】EditText常用属性【
  5. Android的IPC机制
  6. Android Tabhost中tab添加数字demo
  7. 【android】EditText属性大全
  8. android(Import Android demo to Eclipse)
  9. 随笔:Android不爱C++
  10. [Android引擎]Android游戏框架AndE…