Android Dev - 运行 top命令输出到TextView
Android Dev - Run top command output to TextView
我在这个论坛上读了很多书,它对我设置应用程序有很大帮助,但是,我现在还有另一个问题,我想做的是显示 "top" 命令在文本视图中输出。
所以布局基本上是两个按钮(开始和停止)和一个文本视图,我在这里读到我必须使用异步任务来 运行 命令,到这里为止很好,现在的问题是如何解析输出以便正确显示?
我想要的结果是一个显示顶级命令结果的小文本视图(与 运行 在终端中执行命令的输出相同)
public class Async extends AsyncTask<String, String, Void>
{
private View rootView;
private Activity rootAct;
public Async(View view,Activity act) {
// TODO Auto-generated constructor stub\
View rootView = view;
rootAct = act;
}
@Override
protected void onPreExecute() {
Log.d("PRE-EXECUTE", "OK");
}
@Override
protected void onCancelled() {
Log.d("ON CANCEL", "OK");
}
@Override
protected Void doInBackground(String... params) {
//Execution en BackGround
Log.d("BACKGROUND", "START");
Log.d("BACK PARAM 0", params[0]);
Log.d("BACK PARAM 1", params[1]);
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("top");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
publishProgress(output.toString());
}
reader.close();
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void onProgressUpdate(String... values) {
Log.d("UPDATE", "START");
Log.d("RESUL", values[0]);
TextView sortie = (TextView) rootAct.findViewById(R.id.txt_output);
sortie.setText(values[0]);
Log.d("UPDATE", "STOP");
}
@Override
protected void onPostExecute(Void result) {
Log.d("POSTEXE", "OK");
}
}
因此,我想要的是我的 textView 显示最上面的命令结果并在每次存在新的 "starting" 行时更新,但不确定是否很清楚,实际上更简单的示例是打开一个在你的 phone 上输入 top 的终端命令,我想要完全相同的输出...
我希望有人能在那个项目上帮助我! (对不起,我的英语很糟糕,我是法国人 :))
此致,
编辑:事实上,我只需要一种方法来读取缓冲区并将行附加到我的输出,除非连续有 3 个“\n”,这可能吗?
好吧,事实上,下班后我发现的唯一方法就是解析它 "old school" 即使结果并不完美...
while ((line = reader.readLine()) != null) {
if (isCancelled()) {
break;
}
if (line.isEmpty() && i == 2) {
Log.d("BACK", "Retour apres 3 espaces");
publishProgress(output.toString());
output.setLength(0);
i = 0;
} else if (line.isEmpty() && i == 1) {
Log.d("BACK", "2 espaces");
output.append("\n \n");
i = 2;
} else if (line.isEmpty() && i == 0) {
Log.d("BACK", "1 espaces");
output.append("\n \n");
i = 1;
} else {
Log.d("BACK", "LigneTxt");
output.append(line + "\n");
i = 0;
}
这似乎可以完成工作,但是数据流并不是很流畅,因此如果有人有想法,它并不是很完美,我愿意接受任何评论吗?
我在这个论坛上读了很多书,它对我设置应用程序有很大帮助,但是,我现在还有另一个问题,我想做的是显示 "top" 命令在文本视图中输出。 所以布局基本上是两个按钮(开始和停止)和一个文本视图,我在这里读到我必须使用异步任务来 运行 命令,到这里为止很好,现在的问题是如何解析输出以便正确显示? 我想要的结果是一个显示顶级命令结果的小文本视图(与 运行 在终端中执行命令的输出相同)
public class Async extends AsyncTask<String, String, Void>
{
private View rootView;
private Activity rootAct;
public Async(View view,Activity act) {
// TODO Auto-generated constructor stub\
View rootView = view;
rootAct = act;
}
@Override
protected void onPreExecute() {
Log.d("PRE-EXECUTE", "OK");
}
@Override
protected void onCancelled() {
Log.d("ON CANCEL", "OK");
}
@Override
protected Void doInBackground(String... params) {
//Execution en BackGround
Log.d("BACKGROUND", "START");
Log.d("BACK PARAM 0", params[0]);
Log.d("BACK PARAM 1", params[1]);
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("top");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
publishProgress(output.toString());
}
reader.close();
return null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void onProgressUpdate(String... values) {
Log.d("UPDATE", "START");
Log.d("RESUL", values[0]);
TextView sortie = (TextView) rootAct.findViewById(R.id.txt_output);
sortie.setText(values[0]);
Log.d("UPDATE", "STOP");
}
@Override
protected void onPostExecute(Void result) {
Log.d("POSTEXE", "OK");
}
}
因此,我想要的是我的 textView 显示最上面的命令结果并在每次存在新的 "starting" 行时更新,但不确定是否很清楚,实际上更简单的示例是打开一个在你的 phone 上输入 top 的终端命令,我想要完全相同的输出...
我希望有人能在那个项目上帮助我! (对不起,我的英语很糟糕,我是法国人 :))
此致,
编辑:事实上,我只需要一种方法来读取缓冲区并将行附加到我的输出,除非连续有 3 个“\n”,这可能吗?
好吧,事实上,下班后我发现的唯一方法就是解析它 "old school" 即使结果并不完美...
while ((line = reader.readLine()) != null) {
if (isCancelled()) {
break;
}
if (line.isEmpty() && i == 2) {
Log.d("BACK", "Retour apres 3 espaces");
publishProgress(output.toString());
output.setLength(0);
i = 0;
} else if (line.isEmpty() && i == 1) {
Log.d("BACK", "2 espaces");
output.append("\n \n");
i = 2;
} else if (line.isEmpty() && i == 0) {
Log.d("BACK", "1 espaces");
output.append("\n \n");
i = 1;
} else {
Log.d("BACK", "LigneTxt");
output.append(line + "\n");
i = 0;
}
这似乎可以完成工作,但是数据流并不是很流畅,因此如果有人有想法,它并不是很完美,我愿意接受任何评论吗?