1

我不是最好的程序员,实际上,我很糟糕 :( 我需要一些让我发疯的事情的帮助。基本上我有一个 tcpdump 进程,我想提取输出并将其放入一个文本视图中,该文本视图每隔几毫秒,我已经尝试了一切,但无法让它工作。

我没有收到任何错误,它似乎在后台工作,但只有在我转到主屏幕并返回应用程序后才显示文本块。但是,它不会不断更新文本视图,有时会挂起和崩溃。

我创建了一个简单的处理程序,它可以毫无问题地用纯文本更新 textview,但后来我遇到了让它读取进程的主要问题。

开始按钮

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.capture);

    this.LiveTraffic = (TextView) findViewById(R.id.LiveTraffic);
    this.CaptureText = (TextView) findViewById(R.id.CaptureText);
    ((TextView) findViewById(R.id.ipv4)).setText(getLocalIpv4Address());
    ((TextView) findViewById(R.id.ipv6)).setText(getLocalIpv6Address());


    //Begin button              
    final Button startButton = (Button) findViewById(R.id.start);
    startButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Now Capturing Packets", Toast.LENGTH_LONG).show();
            try {

                process = Runtime.getRuntime().exec("su");
                DataOutputStream os = new DataOutputStream(process.getOutputStream());
                os.writeBytes("/data/local/tcpdump -q\n");
                os.flush();
                os.writeBytes("exit\n");
                os.flush();
                os.close();

                inputStream = new DataInputStream(process.getInputStream());

                Thread.sleep(1000);
                Process process2 = Runtime.getRuntime().exec("ps tcpdump");

                DataInputStream in = new DataInputStream(process2.getInputStream());
                String temp = in.readLine();
                temp = in.readLine();
                temp = temp.replaceAll("^root *([0-9]*).*", "$1");
                pid = Integer.parseInt(temp);
                Log.e("MyTemp", "" + pid);
                process2.destroy();

                CaptureActivity.this.thisActivity.CaptureText.setText("Active");
            } catch (Exception e) {
            }
            ListenThread thread = new ListenThread(new BufferedReader(new InputStreamReader(inputStream)));
            thread.start();
        }
    });
}

ListenThread 类

public class ListenThread extends Thread {

    public ListenThread(BufferedReader reader) {
        this.reader = reader;
    }
    private BufferedReader reader = null;

    @Override
    public void run() {

        reader = new BufferedReader(new InputStreamReader(inputStream));
        while (true) {
            try {
                CaptureActivity.this.thisActivity.CaptureText.setText("exec");
                int a = 1;
                String received = reader.readLine();
                while (a == 1) {
                    CaptureActivity.this.thisActivity.LiveTraffic.append(received);
                    CaptureActivity.this.thisActivity.LiveTraffic.append("\n");
                    received = reader.readLine();
                    CaptureActivity.this.thisActivity.CaptureText.setText("in loop");

                }
                CaptureActivity.this.thisActivity.CaptureText.setText("out loop");

            } catch (Exception e) {
                Log.e("FSE", "", e);
            }
        }
    }
}
4

1 回答 1

1

我不是安卓专家,但我注意到:

  • 您正在 UI 线程中运行 I/O 操作 - 这将冻结您的 GUI,直到 I/O 操作完成 ==> 在单独的线程中运行它们。
  • 您从内部的 UI 线程外部更新 UI ListenThread,这可能会导致意外结果

您可以在本教程中阅读有关它的更多信息中阅读更多相关信息(确保您阅读了 2 个示例,因为第一个示例已损坏(故意))。

编辑
总之,你应该在你的第一段代码中有这样的东西:

startButton.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Now Capturing Packets", Toast.LENGTH_LONG).show();
        new Thread(new Runnable() {

            public void run() {
                try {

                    process = Runtime.getRuntime().exec("su");
                    ...
                    CaptureActivity.this.runOnUiThread(new Runnable() {
                        public void run() {
                            CaptureActivity.this.thisActivity.CaptureText.setText("Active");
                        }
                    });
                } catch (Exception e) {
                }
                ListenThread thread = new ListenThread(new BufferedReader(new InputStreamReader(inputStream)));
                thread.start();
            }
        }).start();
    }
});

在第二个中:

   while (true) {
        try {
            CaptureActivity.this.runOnUiThread(new Runnable() {

                public void run() {
                    CaptureActivity.this.thisActivity.CaptureText.setText("exec");
                }
            });

            int a = 1;
            String received = reader.readLine();
            while (a == 1) {
                CaptureActivity.this.runOnUiThread(new Runnable() {

                    public void run() {
                        CaptureActivity.this.thisActivity.LiveTraffic.append(received);
                        CaptureActivity.this.thisActivity.LiveTraffic.append("\n");
                        CaptureActivity.this.thisActivity.CaptureText.setText("in loop");
                    }
                });
                received = reader.readLine();
            }
            CaptureActivity.this.runOnUiThread(new Runnable() {

                public void run() {
                    CaptureActivity.this.thisActivity.CaptureText.setText("out loop");
                }
            });

        } catch (Exception e) {
            Log.e("FSE", "", e);
        }
    }

那应该解决特定的 UI 交互问题。但是您的代码中还有其他逻辑问题超出了这个问题(例如,您从不测试您是否已经到达您正在阅读的文件的末尾,事实上 while(a==1) 是一个无限循环因为你永远不会改变 a 等的值)。

于 2012-06-28T10:22:26.967 回答