appium 源码分析(十二)-pressKeyCode,LongPressKeyCode
来源:互联网 发布:云计算 演讲稿 编辑:程序博客网 时间:2024/05/09 23:01
摘要
有的时候我们想要通过自动化来实现调节手机的音量等其他api未提供的一些方法时候,你就需要通过PressKeyCode以及LongPressKeyCode进行实现了
正文
我们首先先来看下PressKeyCode的源代码吧
public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { try { final Hashtable<String, Object> params = command.params(); Object kc = params.get("keycode"); if (kc instanceof Integer) { keyCode = (Integer) kc; } else if (kc instanceof String) { keyCode = Integer.parseInt((String) kc); } else { throw new IllegalArgumentException("Keycode of type " + kc.getClass() + "not supported."); } if (params.get("metastate") != JSONObject.NULL) { metaState = (Integer) params.get("metastate"); UiDevice.getInstance().pressKeyCode(keyCode, metaState); } else { UiDevice.getInstance().pressKeyCode(keyCode); } return getSuccessResult(true); } catch (final Exception e) { return getErrorResult(e.getMessage()); } }
以上的代码主要及时获取到KeyCode的值以及metastate的值。这里的metastate实际上就是控制按键,比如说shift键,alt键,ctrl键等控制键,获取到两个值以后还是调用的Device实例对象的pressKeyCode方法。
我们还是来一个例子来说明下吧。
self.driver.press_keycode(26)
直接这样子 这里的keycode的话可以通过
Android KeyCode
进行查看,这里的26是power键
下来我们再来看看LongPressKeyCode,因为UiAutomator没有实现相应的API方法,所以只能通过反射的方式调用其中的方法了。 我们来看看LongPressKeyCode
public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { try { InteractionController interactionController = UiAutomatorBridge.getInstance().getInteractionController(); final Hashtable<String, Object> params = command.params(); keyCode = (Integer) params.get("keycode"); metaState = params.get("metastate") != JSONObject.NULL ? (Integer) params .get("metastate") : 0; final long eventTime = SystemClock.uptimeMillis(); // Send an initial down event final KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD); if (interactionController.injectEventSync(downEvent)) { // Send a repeat event. This will cause the FLAG_LONG_PRESS to be set. final KeyEvent repeatEvent = KeyEvent.changeTimeRepeat(downEvent, eventTime, 1); interactionController.injectEventSync(repeatEvent); // Finally, send the up event final KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD); interactionController.injectEventSync(upEvent); } return getSuccessResult(true); } catch (final Exception e) { return getErrorResult(e.getMessage()); } }
前面已经说过了,由于UiAutomator没有实现longPressKeyCode的Api,所以这里的实现需要用到反射的方式,并且它实现了KeyEvent时间,让其重复keyDown一段时间后,再发送keyUp时间。
同样的例子长按power键
self.driver.long_press_keycode(26)
0 0
- appium 源码分析(十二)-pressKeyCode,LongPressKeyCode
- bootstrap之PressKeyCode&&LongPressKeyCode
- Appium源码分析(四)-swipe
- Appium源码分析(五)-drag
- Appium源码分析(七)-click
- Appium源码分析(六)-find(上)
- appium源码分析(六)-find(下)
- appium源码分析(八)-touch
- appium源码分析(九)-getText,setText
- appium源码分析(十)-GetAttribute
- appium源码分析(十一)-getSize,getLocation
- appium源码分析(十三)-UpdateStrings
- Appium Server 源码分析(一)
- appium Bootstrap UiSelectorParser源码分析
- Shark源码分析(十二):线性SVM
- (二十二)Animator 源码分析
- Elasticsearch源码分析十二--过滤器
- Minetest源码分析十二:ServerMap
- 每日一“结”(1)
- 【offer感悟】本科生如何才能进入BAT等一流的互联网公司(软件工程师等研发方向)?
- iOS-CGContextRef画各种图形例子
- C# 将一个对象转换为指定类型
- Masonry
- appium 源码分析(十二)-pressKeyCode,LongPressKeyCode
- hdu 5428 The Factor
- [POJ 1988]Cube Stacking[带权并查集]
- 数据结构面试题1.2.2-下排每个数都是先前上排那十个数在下排出现的次数
- JSON基础
- 尾数相等详解
- vc之CFileDialog打开多个文件
- textfield边框
- Android开发 Lollipop通知栏图片显示异常的原因及处理