【问题标题】:Android Dev - Run top command output to TextViewAndroid Dev - 运行顶部命令输出到 TextView
【发布时间】:2015-05-03 00:45:06
【问题描述】:

我已经阅读了很多这个论坛,它对我设置我的应用程序有很大帮助,但是,我现在确实有另一个问题,我想做的是在 textview 中显示“top”命令输出. 所以布局基本上是两个按钮(开始和停止)和一个textView,我在这里读到我必须使用异步任务来运行命令,到这里为止很好,现在的问题是如何解析输出以便正确显示? 我想要的结果是一个显示顶部命令结果的小文本视图(与在终端中运行命令的输出相同)

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 来显示顶部命令结果并在每次存在新的“起始”行时更新,不确定它是否很清楚,实际上更简单的示例是在您的终端上打开一个终端命令phone type top ,我想要完全相同的输出...

我希望有人能在那个项目上帮助我! (对不起我糟糕的英语,我是法国人:))

最好的问候,

编辑:事实上,我只需要一种方法来读取缓冲区并将行附加到我的输出中,除非连续有 3 个“\n”,这可能吗?

【问题讨论】:

    标签: android android-asynctask textview command output


    【解决方案1】:

    好吧,事实上我发现几个小时后的唯一方法就是解析它“老派”,即使结果并不完美......

    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;
                        }
    

    这似乎完成了这项工作,但是数据流并不是很流畅,因此如果有人有想法,这并不是很完美,我对任何 cmets 持开放态度?

    【讨论】:

      猜你喜欢
      • 2016-03-15
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-27
      • 2014-07-14
      • 1970-01-01
      相关资源
      最近更新 更多