2010-07-28 68 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); 
} 
相關問題