gpt4 book ai didi

android - 如何将 "top -m 10 -n 1"命令输出保存到Android代码中的String?

转载 作者:行者123 更新时间:2023-11-30 03:41:42 24 4
gpt4 key购买 nike

我正在尝试这段代码

public String ReadFromCMD()
{
ProcessBuilder cmd;
String result="";
//int resultshow = 0;

try{
String[] args = {"/system/bin/top -m 10 -n 1"};

cmd = new ProcessBuilder(strCmdArgs);

Process process = cmd.start();
InputStream in = process.getInputStream();


BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();

process.waitFor();

while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();


in.close();

} catch(Exception ex){
ex.printStackTrace();
}
return result;
}

错误来了:

03-25 09:57:36.885: W/System.err(30300): java.io.IOException: Error running exec(). 
Command: [/system/bin/top -m 10 -n 1] Working Directory: null Environment:
[ANDROID_SOCKET_zygote=10, SECONDARY_STORAGE=/storage/extSdCard:/storage/UsbDriveA:/storage
/UsbDriveB:/storage/UsbDriveC:/storage/UsbDriveD:/storage/UsbDriveE:/storage/UsbDriveF,
ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/storage/sdcard0, ANDROID_ASSETS=/system/app,
PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ASEC_MOUNTPOINT=/mnt/asec,
LOOP_MOUNTPOINT=/mnt/obb, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-
junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework
/framework.jar:/system/framework/framework2.jar:/system/framework/android.policy.jar:
/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework
/sec_edm.jar:/system/framework/seccamera.jar, ANDROID_DATA=/data, LD_LIBRARY_PATH=/vendor
/lib:/system/lib, ANDROID_ROOT=/system, ANDROID_PROPERTY_WORKSPACE=9,66560,
VIBE_PIPE_PATH=/dev/pipes]
03-25 09:57:36.885: W/System.err(30300): at java.lang.ProcessManager.exec(ProcessManager.java:211)
03-25 09:57:36.885: W/System.err(30300): at java.lang.ProcessBuilder.start(ProcessBuilder.java:195)

这个错误发生在行

Process process = cmd.start();

请帮我解决这个问题

最佳答案

这个:

String[] args = {"/system/bin/top -m 10 -n 1"};

表示您的可执行文件是目录 /system/bin 中名为 top -m 10 -n 1 的文件。

您想要的是该目录中名为 top 的可执行文件,其中 -m 10 -n 1 作为四个单独的参数:

String[] args = {"/system/bin/top", "-m", "10", "-n", "1"};

或者,由于这些组件都不包含空格,您可以“作弊”一点并编写如下内容:

String[] args = "/system/bin/top -m 10 -n 1".split(" ");

它是一样的(它仍然创建一个包含五个字符串的数组),但可以说更容易阅读。

关于android - 如何将 "top -m 10 -n 1"命令输出保存到Android代码中的String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15607710/

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