2011-08-25 75 views
1

我写了一些运行android命令行并收集输出的代码。程序中的命令行 - 并非所有命令都执行问题

它正确执行“ls”,但是当我把命令“top -n 1”显示什么都不显示。

它是一个明显的问题?这款手机没有扎根,当使用“终端模拟器”时,我可以看到“顶级”输出。

这里是代码:

// ** execute command line and gather the output **// 
    final StringBuilder log = new StringBuilder(); 
    try{ 
     ArrayList<String> commandLine = new ArrayList<String>(); 
     commandLine.add("top"); 
     commandLine.add("-n1"); 

     Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0])); 

     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

     String line; 
     while ((line = bufferedReader.readLine()) != null){ 
      log.append(line); 
      log.append(", \n"); 
     } 
     log.append(", \n"); 
    } 
    catch (IOException e){ 

    } 

感谢, A.

+0

任何代码或日志放弃? – OcuS

+1

我不认为正式支持执行命令行应用程序,这意味着它是不确定它是否工作,特别是跨设备。 – mibollma

+0

好的,我成功运行“ps”,但我得到了很多“根”进程。我如何修剪所有“根”过程。 PS命令有没有后缀? – Karako2017

回答

0

你可能想显示你的一些代码。通常,使用Runtime运行的命令不会在shell中执行,因此您可能想尝试使用“sh -c top -n 1”作为prog参数。

+0

“sh -c top -n1”卡住了我的应用程序.... – Karako2017

+0

那么,您可能想使用调试器来检查它“卡住”的位置。 –