package studyjava; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class monkeytest { public static void main(String[] args) throws IOException, Exception { Process p=Runtime.getRuntime().exec("adb shell pm list packages -3"); //Thread.sleep(3000); InputStream in=p.getInputStream(); InputStreamReader ir=new InputStreamReader(in); BufferedReader br=new BufferedReader(ir); String str; int testnumber=0; while((str=br.readLine())!=null) { String ps=str.trim().split(":")[1]; str=br.readLine(); testnumber+=1; monkeytest(ps); System.out.println(ps+"已跑"); } System.out.println("已跑完,共跑了几个应用的monkey:"+testnumber); } public static void monkeytest(String packagename) throws Exception { Process p=Runtime.getRuntime().exec("adb shell monkey -p"+packagename+" --throttle 300 -v 500"); InputStream in=p.getInputStream(); InputStreamReader ir=new InputStreamReader(in); BufferedReader br=new BufferedReader(ir); String str; while((str=br.readLine())!=null) { if(str.indexOf("Monkey finished")!=-1) { System.out.println(str); break; } else if(str.indexOf("crashed at event")!=-1) { System.out.println(packagename+"报错了"); break; } } } }