0
我想运行诸如ping(或任何其他连续输出)的可执行文件,并读取其输出并将其打印在我的GUI中。从java(Android)运行外部命令并读取连续输出
我用下面的方法来执行我的命令:
command=Runtime.getRuntime().exec("ping www.google.com");
,我读输出如下:
BufferedReader in = new BufferedReader(new InputStreamReader(command.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line+"\n");
}
这工作得很好,虽然显示输出时已经很迟相比,服用时时间我使用adb -d shell执行./excutable。
因为我想这个输出(线)在图形用户界面,所以我做一个TextView和其附加的输出(线):
txtview=(TextView)findViewById(R.id.textview);
while ((line = in.readLine()) != null) {
txtview.append(line+"\n");
System.out.println(line+"\n");
}
但这种崩溃我的应用程序。 请告诉我如何克服这个问题。
但是,我如何读取使用AsyncTask的BufferedReader并在TextView中显示每行(因为它们在BufferedReader中)? – d34th4ck3r
您在doInBackground期间阅读文本。您可以将每行传递给onProgressUpdate并更新其中的UI,也可以更新类成员变量。底线,你*有*做后台线程阅读! – elevine
如何将String作为参数传递给onProgressUpdate? – d34th4ck3r