在上一篇文章《Monkey源码分析之事件注入》中,我们看到了monkey在注入事件的时候用到了《Monkey源码分析番外篇之Android注入事件的三种方法比较》中的第一种方法,通过Internal API的WindowManager的injectKeyEvent之类的方法注入事件。这种方法在android api level 16也就是android4.1.2之后已经发生了变化:

  • 在此之后注入事件的方式变成了使用InputManager的injectInputEvent方法了
  • 而InputManager的getInstance和injectInputEvent等方法后来又变成了隐藏方法,具体哪个版本我没有去查,但起码我现在在看的Android 4.4.2是这样的
  • 同样,uiautomator使用的注入事件方法用的也是InputManager的injectInputEvent的方法,这我想就是为什么UIAutomator只支持api level 16以后的android版本了
这里我们看下monkey在最新的版本API Level 19(android 4.4.2)的注入事件代码。
/*     */   public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) /*     */   { /* 101 */     if (verbose > 1) { String note; /*     */       String note; /* 103 */       if (this.mAction == 1) { /* 104 */         note = "ACTION_UP"; /*     */       } else { /* 106 */         note = "ACTION_DOWN"; /*     */       } /*     */       try /*     */       { /* 110 */         System.out.println(":Sending Key (" + note + "): " + this.mKeyCode + "    // " + MonkeySourceRandom.getKeyName(this.mKeyCode)); /*     */       } /*     */       catch (ArrayIndexOutOfBoundsException e) /*     */       { /* 114 */         System.out.println(":Sending Key (" + note + "): " + this.mKeyCode + "    // Unknown key event"); /*     */       } /*     */     } /*     */      /*     */  /* 119 */     KeyEvent keyEvent = this.mKeyEvent; /* 120 */     if (keyEvent == null) { /* 121 */       long eventTime = this.mEventTime; /* 122 */       if (eventTime <= 0L) { /* 123 */         eventTime = SystemClock.uptimeMillis(); /*     */       } /* 125 */       long downTime = this.mDownTime; /* 126 */       if (downTime <= 0L) { /* 127 */         downTime = eventTime; /*     */       } /* 129 */       keyEvent = new KeyEvent(downTime, eventTime, this.mAction, this.mKeyCode, this.mRepeatCount, this.mMetaState, this.mDeviceId, this.mScanCode, 8, 257); /*     */     } /*     */      /*     */  /* 133 */     if (!InputManager.getInstance().injectInputEvent(keyEvent, 1)) /*     */     { /* 135 */       return 0; /*     */     } /* 137 */     return 1; /*     */   } /*     */ }
可以看到最后的注入事件方法从原来的iwm.injectKeyEvent变成了现在的Inputmanager.getInstance().injectInputEvent方法了。


 

作者

自主博客

微信

CSDN

天地会珠海分舵

http://techgogogo.com


服务号:TechGoGoGo

扫描码:

monkey源码分析之事件注入方法变化_Monkey

  • 收藏
  • 评论
  • 举报
提问和评论都可以,用心的回复会被更多人看到 评论
发布评论
相关文章

举报文章

请选择举报类型

内容侵权 涉嫌营销 内容抄袭 违法信息 其他

具体原因

包含不真实信息 涉及个人隐私

补充说明

0/200

上传截图

格式支持JPEG/PNG/JPG,图片不超过1.9M

已经收到您得举报信息,我们会尽快审核