Android中使用代碼執行shell命令

jopen 11年前發布 | 142K 次閱讀 Android Shell Android開發 移動開發

Android執行shell命令

一、方法
    /**
    * 執行一個shell命令,并返回字符串值
    *
    * @param cmd
    * 命令名稱&參數組成的數組(例如:{"/system/bin/cat", "/proc/version"})
    * @param workdirectory
    * 命令執行路徑(例如:"system/bin/")
    * @return 執行結果組成的字符串
    * @throws IOException
    */
    public static synchronized String run(String[] cmd, String workdirectory)
    throws IOException {
    StringBuffer result = new StringBuffer();
    try {
    // 創建操作系統進程(也可以由Runtime.exec()啟動)
    // Runtime runtime = Runtime.getRuntime();
    // Process proc = runtime.exec(cmd);
    // InputStream inputstream = proc.getInputStream();
    ProcessBuilder builder = new ProcessBuilder(cmd);
    InputStream in = null;
    // 設置一個路徑(絕對路徑了就不一定需要)
    if (workdirectory != null) {
    // 設置工作目錄(同上)
    builder.directory(new File(workdirectory));
    // 合并標準錯誤和標準輸出
    builder.redirectErrorStream(true);
    // 啟動一個新進程
    Process process = builder.start();
    // 讀取進程標準輸出流
    in = process.getInputStream();
    byte[] re = new byte[1024];
    while (in.read(re) != -1) {
    result = result.append(new String(re));
    }
    }
    // 關閉輸入流
    if (in != null) {
    in.close();
    }
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return result.toString();
    } 
二、用途
執行Linux下的top、ps等命令,這些命令你也通過adb可以執行查看效果。
1)top命令如下:
    adb shell
    $ top -h
    top -h
    Usage: top [-m max_procs] [-n iterations] [-d delay] [-s sort_column] [-t] [-h]
    -m num Maximum number of processes to display. // 最多顯示多少個進程
    -n num Updates to show before exiting. // 刷新次數
    -d num Seconds to wait between updates. // 刷新間隔時間(默認5秒)
    -s col Column to sort by <cpu,vss,rss,thr> // 按哪列排序
    -t Show threads instead of processes. // 顯示線程信息而不是進程
    -h Display this help screen. // 顯示幫助文檔
    $ top -n 1
    top -n 1 
就不把執行效果放上來了,總之結果表述如下:
    User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
    User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306 // CPU使用情況
    PID CPU% S #THR VSS RSS PCY UID Name // 進程屬性
    xx xx% x xx xx xx xx xx xx
    CPU占用率:
    User 用戶進程
    System 系統進程
    IOW IO等待時間
    IRQ 硬中斷時間
    CPU使用情況(指一個最小時間片內所占時間,單位jiffies。或者指所占進程數):
    User 處于用戶態的運行時間,不包含優先值為負進程
    Nice 優先值為負的進程所占用的CPU時間
    Sys 處于核心態的運行時間
    Idle 除IO等待時間以外的其它等待時間
    IOW IO等待時間
    IRQ 硬中斷時間
    SIRQ 軟中斷時間
    進程屬性:
    PID 進程在系統中的ID
    CPU% 當前瞬時所以使用CPU占用率
    S 進程的狀態,其中S表示休眠,R表示正在運行,Z表示僵死狀態,N表示該進程優先值是負數。
    #THR 程序當前所用的線程數
    VSS Virtual Set Size 虛擬耗用內存(包含共享庫占用的內存)
    RSS Resident Set Size 實際使用物理內存(包含共享庫占用的內存)
    PCY OOXX,不知道什么東東
    UID 運行當前進程的用戶id
    Name 程序名稱android.process.media
    // ps:內存占用大小有如下規律:VSS >= RSS >= PSS >= USS
    // PSS Proportional Set Size 實際使用的物理內存(比例分配共享庫占用的內存)
    // USS Unique Set Size 進程獨自占用的物理內存(不包含共享庫占用的內存) 

在附件Android系統->android top.txt文件內,自個總結的。

2)執行代碼
    // top命令
    public static final String[] TOP = { "/system/bin/top", "-n", "1" };
    // 現在執行top -n 1,我們只需要第二行(用第二行求得CPU占用率,精確數據)
    // 第一行:User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率
    // 第二行:User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306
    // // CPU使用情況
    public static synchronized String run(String[] cmd) {
    String line = "";
    InputStream is = null;
    try {
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec(cmd);
    is = proc.getInputStream();
    // 換成BufferedReader
    BufferedReader buf = new BufferedReader(new InputStreamReader(is));
    do {
    line = buf.readLine();
    // 前面有幾個空行
    if (line.startsWith("User")) {
    // 讀到第一行時,我們再讀取下一行
    line = buf.readLine();
    break;
    }
    } while (true);
    if (is != null) {
    buf.close();
    is.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    return line;
    }
    // 獲取指定應用的top命令獲取的信息
    // PID CPU% S #THR VSS RSS PCY UID Name // 進程屬性
    // 如果當前應用不在運行則返回null
    public static synchronized String run(String[] cmd, String pkgName) {
    String line = null;
    InputStream is = null;
    try {
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec(cmd);
    is = proc.getInputStream();
    // 換成BufferedReader
    BufferedReader buf = new BufferedReader(new InputStreamReader(is));
    do {
    line = buf.readLine();
    // 讀取到相應pkgName跳出循環(或者未找到)
    if (null == line || line.endsWith(pkgName)) {
    break;
    }
    } while (true);
    if (is != null) {
    buf.close();
    is.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    return line;
    } 
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!