gpt4 book ai didi

android - 执行 shell 命令并在 TextView 中获取输出

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:50 25 4
gpt4 key购买 nike

我想执行一些 shell 命令 并在 TextView 中获取输出。该命令可能有一个连续的输出,如 ping 或 logcat。此外,TextView 应在实时添加命令输出时自动滚动。为此,我这样做了:

package com.example.rootapp;

import java.io.DataOutputStream;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] cmdArray={"logcat -b radio"};
try {
runAsRoot(cmdArray);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void runAsRoot(String[] cmds) throws Exception {
tv=(TextView)findViewById(R.id.cmdOp);
tv.setMovementMethod(new ScrollingMovementMethod());
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
InputStream is = p.getInputStream();
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
int readed = 0;
byte[] buff = new byte[4096];
boolean cmdRequiresAnOutput = true;
if (cmdRequiresAnOutput) {
while( is.available() <= 0) {
try { Thread.sleep(5000); } catch(Exception ex) {}
}

while( is.available() > 0) {
readed = is.read(buff);
if ( readed <= 0 ) break;
String seg = new String(buff,0,readed);
tv.append(seg);
}
}
}
}
}

这工作正常,但它不会连续更新 TextView。如您所见,我正在以 root 用户身份执行 radio logcat,输出不断生成(已在终端仿真器和 adb shell 中检查过)。但就我而言,输出不是连续添加的。它在几百行后停止。这是我的布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/cmdOp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="@string/hello_world" />
</ScrollView>

</RelativeLayout>

输出应该继续滚动 TextView。至少这是我所期望的……请问有什么解决方法吗?

最佳答案

您可以运行命令并将命令输出显示为文本,如下所示:

public class MainActivity extends Activity {

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.cmdOp);
tv.setText("Output :"+"\n"+runAsRoot());
}

public String runAsRoot() {

try {
// Executes the command.
Process process = Runtime.getRuntime().exec("ls -l");

// 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);
}
reader.close();

// Waits for the command to finish.
process.waitFor();

return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

注意:“su”命令仅在设备已获得 root 权限时运行。否则会抛出异常。

关于android - 执行 shell 命令并在 TextView 中获取输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23608005/

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