2010-07-28 89 views

回答

33

查看日志收集器作为示例。这里是relevant file

的关键是在这里:

ArrayList<String> commandLine = new ArrayList<String>(); 
commandLine.add("logcat");//$NON-NLS-1$ 
[...] 

Process process = Runtime.getRuntime().exec(commandLine); 
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
+0

所以基本上,如果我想上运行,我可以只需用顶部替换commandLine,它应该正常工作吗?我得到一个错误,所以我想我需要更多的帮助..谢谢.. – Shouvik 2010-07-28 11:40:57

+0

我是否需要包围进程process = Runtime.getRuntime()。exec(commandLine);尝试和赶上,因为我不断收到这抛出IOException。我看了java的例子似乎处理它的方式... – Shouvik 2010-07-28 12:33:06

+0

是的..再次,看看链接,看看他们做的一切。你肯定需要做错误检查 - 不要指望'顶部'出现在所有手机上。 – EboMike 2010-07-28 16:15:07

5

这也取决于你在终端运行的是什么...如果你是在你也可以做这样的一个文件上运行“猫”。

final private String MEM_FILE = "/proc/meminfo"; 

public Long readMem() { 
    String[] segs; 
    FileReader fstream; 
    try { 
     fstream = new FileReader(MEM_FILE); 
    } catch (FileNotFoundException e) { 
     Log.e("readMem", "Could not read " + MEM_FILE); 
     return false; 
    } 
    BufferedReader in = new BufferedReader(fstream, 500); 
    String line; 
    try { 
     while ((line = in.readLine()) != null) { 
      if (line.indexOf("MemTotal:") > 0) { 
       Log.e("MemTotal", line); 
       segs = line.trim().split("[ ]+"); 
       memTotal = Long.parseLong(segs[1]); 
      } 
      if (line.indexOf("MemFree:") > 0) { 
       Log.e("MemFree", line); 
       segs = line.trim().split("[ ]+"); 
       memFree = Long.parseLong(segs[1]); 
      } 
     } 
     updateMem(); //call function to update textviews or whatever 
     return true; 
    } catch (IOException e) { 
     Log.e("readMem", e.toString()); 
    } 
    return false; 
} 

编辑: 有一个名为netmeter的Android实验项目给你一个很好的例子。有一个名为Top.java的类实际上完全按照您的需要进行,并将其用于TaskList.java中以供显示。 http://code.google.com/p/android-labs/source/browse/#svn/trunk/NetMeter/src/com/google/android/netmeter

+0

不是猫,我特别想跑到顶端,并获得屏幕上显示的实时结果。我知道模拟终端的应用程序,我发现它的顶级工作,所以我想知道如何才能拉出顶部的结果并在UI上显示它们。 – Shouvik 2010-07-28 11:33:50

+0

查看我上面的编辑,了解您想要的正确工作示例。 – androidworkz 2010-07-28 12:42:31

+0

嘿谢谢...需要在这里度过一段时间,但我认为这是...你一直在帮助很大! – Shouvik 2010-07-29 05:05:58

15

好吧,这到底是什么,以防万一在尝试为我工作的人需要它在未来... :)

环绕,赶上

try { 
    Process process = Runtime.getRuntime().exec("top -n 1 -d 1"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); 
} catch (InterruptedException e) { 
    e.printStackTrace(); 
} 
+1

当我这样做后跟String result = null; result = bufferedReader.readLine(); Log.i(“OnClickListener”,“result:”+ result);它只显示“result:”('result'字符串为空)。任何想法,为什么会这样?另外,当我尝试process = Runtime.getRuntime()。exec(“/ system/bin/ping abc”);相反,'结果'字符串为空。这两个命令都可以像'adb shell'终端一样按预期工作。 – 2013-09-17 17:15:40

+0

对不起@DavidDoria,我现在两年没碰过android。这是我在大学期间尝试过的一些事情。你最好把它作为一个问题发布。 – Shouvik 2013-09-20 16:01:13

+0

我得到它的工作。问题是,'top'的第一行是空的(一个新行,创建表)。实际输出时它返回null的情况是因为我需要得到stderror(getErrorStream)而不是stdout(getInputStream)。 – 2013-09-20 17:38:17

8

我们,可以执行命令,跟着,我成功地做到了这一点....!像这样尝试,这里我们需要指定完整的命令路径。获得条命令的完整路径,在乌尔终端(机器人)型

* $其中LS

/系统/箱*

try { 

    // Executes the command. 

    Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard"); 

    // 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); 
} 
相关问题