标签:
在事件生成并放入到命令队列后,Monkey类的runMonkeyCycles就会去调用相应事件源的getNextEvent来获的事件来执行事件注入,那么这一小节我们通过MonkeyKeyEvent这个事件的注入方法来看下事件注入过程是怎么样的。
往系统注入按键事件最终是通过调用InputManager提供的方法来实现的,在Android系统中,按键事件是由InputManager来收集并由WindowManagerService服务来分发给各个Activity处理的,这个系统服务。它是用于管理整个系统的输入部分,包括键盘、鼠标、触摸屏等等。所以MonkeyKeyEvent往系统注入按键事件要做的事情就是要准备好InputManager注入事件的方法 injectInputEvent所需要的参数KeyEvent实例。而KeyEvent实例或者用于构建KeyEvent实例所需要用到的参数在MonkeyKeyEvent的成员变量中都有定义:
27 public class MonkeyKeyEvent 28 extends MonkeyEvent 29 { 30 private int mDeviceId; 31 private long mEventTime; 32 private long mDownTime; 33 private int mAction; 34 private int mKeyCode; 35 private int mScanCode; 36 private int mMetaState; 37 private int mRepeatCount; 38 private KeyEvent mKeyEvent;代码6-7-1 MonkeyKeyEvent 成员变量
以下我们先简要描述下各个成员变量的意义,其中大部分变量都是用于构建KeyEvent用的:
MonkeyKeyEvent支持多个构造函数,其中有两个比较重要。调用者可以传入除mKeyEvent外的所有其他变量进行初始化,也可以直接传入一个KeyEvent实例进行初始化,因为刚才说过了KeyEvent里面包含了所有其他变量。我们往下看下者两个构造函数:
46 public MonkeyKeyEvent(long downTime, long eventTime, int action, int keyCode, int repeatCount, int metaState, int device, int scan Code) 47 { 48 super(0); 49 this.mDownTime = downTime; 50 this.mEventTime = eventTime; 51 this.mAction = action; 52 this.mKeyCode = keyCode; 53 this.mRepeatCount = repeatCount; 54 this.mMetaState = metaState; 55 this.mDeviceId = device; 56 this.mScanCode = scanCode; 57 } 58 59 public MonkeyKeyEvent(KeyEvent e) { 60 super(0); 61 this.mKeyEvent = e; 62 }代码6-7-2 MonkeyKeyEvent构造函数
在准备好注入事件所需要的提供的信息后,下一步就需要去看下MonkeyKeyEvent的注入事件这个方法是怎么实现的了:
99 @Override 100 public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) { 101 if (verbose > 1) { 102 String note; 103 if (mAction == KeyEvent.ACTION_UP) { 104 note = "ACTION_UP"; 105 } else { 106 note = "ACTION_DOWN"; 107 } 108 109 try { 110 System.out.println(":Sending Key (" + note + "): " 111 + mKeyCode + " // " 112 + MonkeySourceRandom.getKeyName(mKeyCode)); 113 } catch (ArrayIndexOutOfBoundsException e) { 114 System.out.println(":Sending Key (" + note + "): " 115 + mKeyCode + " // Unknown key event"); 116 } 117 } 118 119 KeyEvent keyEvent = mKeyEvent; 120 if (keyEvent == null) { 121 long eventTime = mEventTime; 122 if (eventTime <= 0) { 123 eventTime = SystemClock.uptimeMillis(); 124 } 125 long downTime = mDownTime; 126 if (downTime <= 0) { 127 downTime = eventTime; 128 } 129 keyEvent = new KeyEvent(downTime, eventTime, mAction, mKeyCode, 130 mRepeatCount, mMetaState, mDeviceId, mScanCode, 131 KeyEvent.FLAG_FROM_SYSTEM, InputDevice.SOURCE_KEYBOARD); 132 } 133 if (!InputManager.getInstance().injectInputEvent(keyEvent, 134 InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT)) { 135 return MonkeyEvent.INJECT_FAIL; 136 } 137 return MonkeyEvent.INJECT_SUCCESS; 138 } 139 }代码6-7-3 MonkeyKeyEvent - injectEvent
注:更多文章请关注公众号:techgogogo或个人博客http://techgogogo.com。当然,也非常欢迎您直接微信(zhubaitian1)勾搭。本文由天地会珠海分舵原创。转载请自觉,是否投诉维权看心情。
第6章7节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-注入按键事件实例
标签:
原文地址:http://blog.csdn.net/zhubaitian/article/details/50212663