1. 程式人生 > >Android中使用程式碼執行shell命令

Android中使用程式碼執行shell命令

一、方法
  1. /**
  2. * 執行一個shell命令,並返回字串值
  3. *
  4. * @param cmd
  5. * 命令名稱&引數組成的陣列(例如:{"/system/bin/cat", "/proc/version"})
  6. * @param workdirectory
  7. * 命令執行路徑(例如:"system/bin/")
  8. * @return 執行結果組成的字串
  9. * @throws IOException
  10. */
  11. publicstaticsynchronized String run(String[] cmd, String workdirectory)
  12. throws IOException {
  13. StringBuffer result = new StringBuffer();
  14. try {
  15. // 建立作業系統程序(也可以由Runtime.exec()啟動)
  16. // Runtime runtime = Runtime.getRuntime();
  17. // Process proc = runtime.exec(cmd);
  18. // InputStream inputstream = proc.getInputStream();
  19. ProcessBuilder builder = new ProcessBuilder(cmd);
  20. InputStream in = null;
  21. // 設定一個路徑(絕對路徑了就不一定需要)
  22. if (workdirectory != null) {
  23. // 設定工作目錄(同上)
  24. builder.directory(new File(workdirectory));
  25. // 合併標準錯誤和標準輸出
  26. builder.redirectErrorStream(true);
  27. // 啟動一個新程序
  28. Process process = builder.start();
  29. // 讀取程序標準輸出流
  30. in = process.getInputStream();
  31. byte[] re = newbyte[1024];
  32. while (in.read(re) != -1) {
  33. result = result.append(new String(re));
  34. }
  35. }
  36. // 關閉輸入流
  37. if (in != null) {
  38. in.close();
  39. }
  40. } catch (Exception ex) {
  41. ex.printStackTrace();
  42. }
  43. return result.toString();
  44. }
二、用途 執行Linux下的top、ps等命令,這些命令你也通過adb可以執行檢視效果。 1)top命令如下:
  1. adb shell
  2. $ top -h
  3. top -h
  4. Usage: top [-m max_procs] [-n iterations] [-d delay] [-s sort_column] [-t] [-h]
  5. -m num Maximum number of processes to display. // 最多顯示多少個程序
  6. -n num Updates to show before exiting. // 重新整理次數
  7. -d num Seconds to wait between updates. // 重新整理間隔時間(預設5秒)
  8. -s col Columnto sort by <cpu,vss,rss,thr> // 按哪列排序
  9. -t Show threads insteadof processes. // 顯示執行緒資訊而不是程序
  10. -h Display this help screen. // 顯示幫助文件
  11. $ top -n 1
  12. top -n 1
就不把執行效果放上來了,總之結果表述如下:
  1. User 35%, System 13%, IOW 0%, IRQ 0% // CPU佔用率
  2. User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306 // CPU使用情況
  3. PID CPU% S #THR VSS RSS PCY UID Name // 程序屬性
  4. xx xx% x xx xx xx xx xx xx
  5. CPU佔用率:
  6. User 使用者程序
  7. System 系統程序
  8. IOW IO等待時間
  9. IRQ 硬中斷時間
  10. CPU使用情況(指一個最小時間片內所佔時間,單位jiffies。或者指所佔程序數):
  11. User 處於使用者態的執行時間,不包含優先值為負程序
  12. Nice 優先值為負的程序所佔用的CPU時間
  13. Sys 處於核心態的執行時間
  14. Idle 除IO等待時間以外的其它等待時間
  15. IOW IO等待時間
  16. IRQ 硬中斷時間
  17. SIRQ 軟中斷時間
  18. 程序屬性:
  19. PID 程序在系統中的ID
  20. CPU% 當前瞬時所以使用CPU佔用率
  21. S 程序的狀態,其中S表示休眠,R表示正在執行,Z表示僵死狀態,N表示該程序優先值是負數。
  22. #THR 程式當前所用的執行緒數
  23. VSS Virtual SetSize 虛擬耗用記憶體(包含共享庫佔用的記憶體)
  24. RSS Resident SetSize 實際使用實體記憶體(包含共享庫佔用的記憶體)
  25. PCY OOXX,不知道什麼東東
  26. UID 運行當前程序的使用者id
  27. Name 程式名稱android.process.media
  28. // ps:記憶體佔用大小有如下規律:VSS >= RSS >= PSS >= USS
  29. // PSS Proportional SetSize 實際使用的實體記憶體(比例分配共享庫佔用的記憶體)
  30. // USS UniqueSetSize 程序獨自佔用的實體記憶體(不包含共享庫佔用的記憶體)

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

2)執行程式碼
  1. // top命令
  2. publicstaticfinal String[] TOP = { "/system/bin/top", "-n", "1" };
  3. // 現在執行top -n 1,我們只需要第二行(用第二行求得CPU佔用率,精確資料)
  4. // 第一行:User 35%, System 13%, IOW 0%, IRQ 0% // CPU佔用率
  5. // 第二行:User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306
  6. // // CPU使用情況
  7. publicstaticsynchronized String run(String[] cmd) {
  8. String line = "";
  9. InputStream is = null;
  10. try {
  11. Runtime runtime = Runtime.getRuntime();
  12. Process proc = runtime.exec(cmd);
  13. is = proc.getInputStream();
  14. // 換成BufferedReader
  15. BufferedReader buf = new BufferedReader(new InputStreamReader(is));
  16. do {
  17. line = buf.readLine();
  18. // 前面有幾個空行
  19. if (line.startsWith("User")) {
  20. // 讀到第一行時,我們再讀取下一行
  21. line = buf.readLine();
  22. break;
  23. }
  24. } while (true);
  25. if (is != null) {
  26. buf.close();
  27. is.close();
  28. }
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. return line;
  33. }
  34. // 獲取指定應用的top命令獲取的資訊
  35. // PID CPU% S #THR VSS RSS PCY UID Name // 程序屬性
  36. // 如果當前應用不在執行則返回null
  37. publicstaticsynchronized String run(String[] cmd, String pkgName) {
  38. String line = null;
  39. InputStream is = null;
  40. try {
  41. Runtime runtime = Runtime.getRuntime();
  42. Process proc = runtime.exec(cmd);
  43. is = proc.getInputStream();
  44. // 換成BufferedReader
  45. BufferedReader buf = new BufferedReader(new InputStreamReader(is));
  46. do {
  47. line = buf.readLine();
  48. // 讀取到相應pkgName跳出迴圈(或者未找到)
  49. if (null == line || line.endsWith(pkgName)) {
  50. break;
  51. }
  52. } while (true);
  53. if (is != null) {
  54. buf.close();
  55. is.close();
  56. }
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. return line;
  61. }