1、首先布局
2、最关键的主要是封装将monkey指令封装好
3、使用button监听指令
如下:
//执行monkey
public void Monkey (String PackageName, String crashs, String timeout, String security,
String natives, String killprocess, String throttle,String touch, String trackball,
String systemkey, String anyevent, String seed, String testtime,String path){
Runtime runtime = Runtime.getRuntime();
String cmd = "adb shell monkey -p " + PackageName + " " + crashs + timeout + security + natives + killprocess +
"--throttle " +throttle+ " --pct-touch "+touch + " --pct-trackball " + trackball
+ " --pct-syskeys " +systemkey + " --pct-anyevent " + anyevent + " -s " + seed + " " + testtime + " > " + path;
try {
String[] args = {"cmd","/c",cmd};
Process process = runtime.exec(args);
System.out.println(cmd);
try {
Thread.sleep(2000);
} catch (Exception e) {
}
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}
}
//停止monkey
public void stopMonkey(){
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec("adb shell top | grep monkey");
BufferedReader bReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String pid1 = null;
String pid = null;
String pattern = "(\\d+\\s+)(\\d+\\s+)(\\d+)%";
if ((pid1 = bReader.readLine()) != null) {
// System.out.println(pid1);
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(pid1);
if (m.find()) {
// System.out.println("pid为:" + m.group(1));
pid = m.group(1).trim();
runtime.exec("adb shell kill -9 " + pid);
}
}
process.destroy();
}
catch (IOException e) {
e.printStackTrace();
}
}