Skip to content

Instantly share code, notes, and snippets.

@trongvu
Created February 17, 2019 11:51
Show Gist options
  • Save trongvu/b1b36305dd3a6faae5eac51bc4b94145 to your computer and use it in GitHub Desktop.
Save trongvu/b1b36305dd3a6faae5eac51bc4b94145 to your computer and use it in GitHub Desktop.
Runtime.exec
public void onCLick(View view) {
String cmd = "monkey -p com.google.android.calculator --throttle 200 -v 10000";
try {
final Process p = Runtime.getRuntime().exec(cmd);
new Thread(() -> {
try {
InputStreamReader isr = new InputStreamReader (p.getInputStream());
BufferedReader br = new BufferedReader(isr);
while (true) {
String s = br.readLine ();
if (s == null) break;
System.out.println (s);
}
p.getInputStream().close ();
} catch (Exception ex) {
System.out.println ("Problem reading stream getInputStream: " + ex);
ex.printStackTrace ();
}
}).run();
new Thread(() -> {
try {
InputStreamReader isr = new InputStreamReader (p.getErrorStream());
BufferedReader br = new BufferedReader(isr);
while (true) {
String s = br.readLine ();
if (s == null) break;
System.out.println (s);
}
p.getInputStream().close ();
} catch (Exception ex) {
System.out.println ("Problem reading stream getErrorStream: " + ex);
ex.printStackTrace ();
}
}).run();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment