gpt4 book ai didi

android - 如何将流程输出链接到textview并实时自动更新?

转载 作者:行者123 更新时间:2023-11-30 04:08:26 25 4
gpt4 key购买 nike

我不是最好的程序员,事实上,我很糟糕:(我需要帮助来解决让我发疯的事情。基本上我有一个 tcpdump 进程,我想提取输出并将其放入每隔几毫秒更新一次的 TextView 中,我已经尝试了所有方法但无法让它工作。

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

我创建了一个简单的处理程序,它可以毫无问题地用纯文本更新 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);
}
}
}
}

最佳答案

我不是 Android 专家,但我注意到:

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

您可以在 this tutorial 中阅读更多相关信息(确保您阅读了 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 等的值)。

关于android - 如何将流程输出链接到textview并实时自动更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11241737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com