I want to run excutable such as ping(or any other which gives continuous output) and read its output and print it in my GUI.
I used following method to run my commands:
command=Runtime.getRuntime().exec("ping www.google.com");
and I am reading the output as following:
BufferedReader in = new BufferedReader(new InputStreamReader(command.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line+"\n");
}
this works fine , although display output pretty late compared to time taken when i execute ./excutable using adb -d shell.
As I want to this output(line) in gui so I make a TextView and append the output(line) in it as:
txtview=(TextView)findViewById(R.id.textview);
while ((line = in.readLine()) != null) {
txtview.append(line+"\n");
System.out.println(line+"\n");
}
but this crashes my application.
please tell me how to get over this problem.
If your last block of code is being executed in an
Activity, then you are probably blocking the UI thread for too long. This will cause Android to force close your application. Try usingAsyncTaskto get the output and update your GUI.