最近在用MR写脚本时,遇到长按的问题,这问题困惑了我许久,最后通过monkeyrunner的源码及官网的那简单的不能再简单的例子,让我恍然大悟。
先上代码:
1、长按错误的代码:
device.touch(xPoint,yPoint,‘UP’)
mr.sleep(sTime)
device.touch(xPoint,yPoint,'DOWN')
2、正确的代码:
device.touch(xPoint,yPoint,md.DOWN_AND_UP)
mr.sleep(sTime)
device.touch(xPoint,yPoint,md.DOWN_AND_UP)
以上的区别在touch()的第二参数的区别,就是这个小错误,导致我百思不得其解,因为拿来主意,说说区别,首先我们先看touch()的源码:
@MonkeyRunnerExported(doc = "Sends a touch event at the specified location",
args = { "x", "y", "type" },
argDocs = { "x coordinate in pixels",
"y coordinate in pixels",
"touch event type as returned by TouchPressType()"})
public void touch(PyObject[] args, String[] kws) {
ArgParser ap = JythonUtils.createArgParser(args, kws);
Preconditions.checkNotNull(ap);
int x = ap.getInt(0);
int y = ap.getInt(1);
TouchPressType type = TouchPressType.fromIdentifier(ap.getString(2));
if (type == null) {
LOG.warning(String.format("Invalid TouchPressType specified (%s) default used instead",
ap.getString(2)));
type = TouchPressType.DOWN_AND_UP;
}
impl.touch(x, y, type);
}
就这段了,通过源码我才知道,原来参数touch()的第二参数可以是任务字符都不会报错,如果不是需要值,则默认为TouchPressType.DOWN_AND_UP。
这让我们知道了第一参数应该是MonkeyDeive.UP,MonkeyDevice.DOWN,MonkeyDeive.DOWN_AND_UP;
通过monkeyrunner打印得到字符串值为up,down,downAndUp.
以上引以为戒,官方文档还是得认真看,希望能告诫正在学习monkeyrunner的同学们。
先上代码:
1、长按错误的代码:
device.touch(xPoint,yPoint,‘UP’)
mr.sleep(sTime)
device.touch(xPoint,yPoint,'DOWN')
2、正确的代码:
device.touch(xPoint,yPoint,md.DOWN_AND_UP)
mr.sleep(sTime)
device.touch(xPoint,yPoint,md.DOWN_AND_UP)
以上的区别在touch()的第二参数的区别,就是这个小错误,导致我百思不得其解,因为拿来主意,说说区别,首先我们先看touch()的源码:
@MonkeyRunnerExported(doc = "Sends a touch event at the specified location",
args = { "x", "y", "type" },
argDocs = { "x coordinate in pixels",
"y coordinate in pixels",
"touch event type as returned by TouchPressType()"})
public void touch(PyObject[] args, String[] kws) {
ArgParser ap = JythonUtils.createArgParser(args, kws);
Preconditions.checkNotNull(ap);
int x = ap.getInt(0);
int y = ap.getInt(1);
TouchPressType type = TouchPressType.fromIdentifier(ap.getString(2));
if (type == null) {
LOG.warning(String.format("Invalid TouchPressType specified (%s) default used instead",
ap.getString(2)));
type = TouchPressType.DOWN_AND_UP;
}
impl.touch(x, y, type);
}
就这段了,通过源码我才知道,原来参数touch()的第二参数可以是任务字符都不会报错,如果不是需要值,则默认为TouchPressType.DOWN_AND_UP。
这让我们知道了第一参数应该是MonkeyDeive.UP,MonkeyDevice.DOWN,MonkeyDeive.DOWN_AND_UP;
通过monkeyrunner打印得到字符串值为up,down,downAndUp.
以上引以为戒,官方文档还是得认真看,希望能告诫正在学习monkeyrunner的同学们。









