如何将进程的标准输出绑定到 TextView

发布于 2024-10-18 09:19:40 字数 621 浏览 3 评论 0原文

我正在开发我的第一个 Android 应用程序。我需要以 root 用户身份在 shell 中执行命令,因此我在我的应用程序中引入了以下代码:

process = Runtime.getRuntime().exec("su");

然后我获得进程的输出流并使用它来执行命令:

os = new DataOutputStream(process.getOutputStream());

os.writeBytes("tcpdump\n");

然后我获得一个输入流想要用于显示过程的结果:

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

我想将获得的 DataInputStream 绑定到布局中的 TextView,以便它显示的文本随着过程继续显示结果而实时更新。

我一直在搜索 android 的 java.io API,但找不到一个简单的方法来做到这一点。我一直在考虑运行一个带有循环的线程,该循环不断检查输入流中是否有新数据,然后将其复制到 TextView,但这似乎是一个蹩脚的解决方案。

如果有人知道如何做到这一点,我会感谢你。

I'm developping my first Android App. I need to execute a command in a shell as the root user so I've introduced this code in my App:

process = Runtime.getRuntime().exec("su");

Then I obtain an output stream to the process and I use it to execute the command:

os = new DataOutputStream(process.getOutputStream());

os.writeBytes("tcpdump\n");

Then I obtain an input stream which I want to use for displaying the results of the process:

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

I would like to bind the obtained DataInputStream to a TextView in the layout so the text that it displays gets updated in real time as the process goes on showing results.

I've been searching trought the java.io API for android and I can't find an easy way to do this. I've been thinking in running a thread with a loop which continously checks if there is new data in the input stream and then copy it to the TextView but this seems a crappy solution.

I would thank you if anyone knows a way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

谢绝鈎搭 2024-10-25 09:19:40
Channel channel = session.openChannel("shell");

OutputStream os = new OutputStream() {

    @Override
    public void write(int oneByte) throws IOException {
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        char ch = new Character((char) oneByte);
        textView1.setText(textView1.getText() + String.valueOf(ch));
        System.out.write(oneByte);
    }
};

// channel.setOutputStream(System.out);
channel.setOutputStream(os, true);
Channel channel = session.openChannel("shell");

OutputStream os = new OutputStream() {

    @Override
    public void write(int oneByte) throws IOException {
        TextView textView1 = (TextView) findViewById(R.id.textView1);
        char ch = new Character((char) oneByte);
        textView1.setText(textView1.getText() + String.valueOf(ch));
        System.out.write(oneByte);
    }
};

// channel.setOutputStream(System.out);
channel.setOutputStream(os, true);
半枫 2024-10-25 09:19:40

将 TextView.append 方法与 Handler 结合起来

这是一个很好的例子:

http:// /android-developers.blogspot.com/2007/11/stitch-in-time.html

Combine TextView.append method with Handler

Here is good example:

http://android-developers.blogspot.com/2007/11/stitch-in-time.html

扭转时空 2024-10-25 09:19:40

我已经能够设置一个处理程序并启动一个可运行的程序,它将每 200 毫秒从输入流中读取一次。

尽管如此,输入流似乎没有从进程标准输出接收任何字符,每次我调用 read() 方法时,它都会被阻塞,等待永远不会出现的字符。我一直在尝试遵循这两个网站的说明,但没有成功:

http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App

http://code.google.com/p/market-enabler/wiki/ShellCommands

谢谢。

I've been able to set a Handler and start a runnable which will read from the input stream every 200ms.

Despite this, it seems that the input stream isn't receiving any characters form the process standard output and everytime I call a read() method it gets blocked waiting for characters that never come. I've been trying following this two websites instructions without sucess:

http://gimite.net/en/index.php?Run%20native%20executable%20in%20Android%20App

http://code.google.com/p/market-enabler/wiki/ShellCommands

Thanks.

烈酒灼喉 2024-10-25 09:19:40

经过一天的研究和测试,我发现解决这个问题的唯一方法是使用AsyncTask。
您可以调整此代码,该代码工作正常:https://stackoverflow.com/a/9063257
如果您喜欢的话,它也可以用 Runtime.getRuntime() 替换 ProcessBuilder() 。

After one day of research and tests, I found that the only solution for this problem is using AsyncTask.
You can adapt this code, which is working fine: https://stackoverflow.com/a/9063257
It also works replacing ProcessBuilder() with Runtime.getRuntime() if you like it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文