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     public static synchronized 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      
21             InputStream in = null; 
22             // 设置一个路径(绝对路径了就不一定需要) 
23             if (workdirectory != null) { 
24                 // 设置工作目录(同上) 
25                 builder.directory(new File(workdirectory)); 
26                 // 合并标准错误和标准输出 
27                 builder.redirectErrorStream(true); 
28                 // 启动一个新进程 
29                 Process process = builder.start(); 
30      
31                 // 读取进程标准输出流 
32                 in = process.getInputStream(); 
33                 byte[] re = new byte[1024]; 
34                 while (in.read(re) != -1) { 
35                     result = result.append(new String(re)); 
36                 } 
37             } 
38             // 关闭输入流 
39             if (in != null) { 
40                 in.close(); 
41             } 
42         } catch (Exception ex) { 
43             ex.printStackTrace(); 
44         } 
45         return result.toString(); 
46     } 

二、用途
执行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  Column to sort by <cpu,vss,rss,thr> // 按哪列排序 
 9       -t      Show threads instead of 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      
 4     PID CPU% S #THR VSS RSS PCY UID Name // 进程属性 
 5     xx  xx% x   xx  xx  xx  xx  xx   xx 
 6      
 7     CPU占用率: 
 8     User    用户进程 
 9     System  系统进程 
10     IOW IO等待时间 
11     IRQ 硬中断时间 
12      
13     CPU使用情况(指一个最小时间片内所占时间,单位jiffies。或者指所占进程数): 
14     User    处于用户态的运行时间,不包含优先值为负进程 
15     Nice    优先值为负的进程所占用的CPU时间 
16     Sys 处于核心态的运行时间 
17     Idle    除IO等待时间以外的其它等待时间 
18     IOW IO等待时间 
19     IRQ 硬中断时间 
20     SIRQ    软中断时间 
21      
22     进程属性: 
23     PID 进程在系统中的ID 
24     CPU%    当前瞬时所以使用CPU占用率 
25     S   进程的状态,其中S表示休眠,R表示正在运行,Z表示僵死状态,N表示该进程优先值是负数。 
26     #THR    程序当前所用的线程数 
27     VSS Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) 
28     RSS Resident Set Size 实际使用物理内存(包含共享库占用的内存) 
29     PCY OOXX,不知道什么东东 
30     UID 运行当前进程的用户id 
31     Name    程序名称android.process.media 
32      
33     // ps:内存占用大小有如下规律:VSS >= RSS >= PSS >= USS 
34     // PSS  Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存) 
35     // USS  Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存) 

在附件Android系统->android top.txt文件内,自个总结的。

 2)执行代码

 1     // top命令 
 2     public static final String[] TOP = { "/system/bin/top", "-n", "1" }; 
 3      
 4     // 现在执行top -n 1,我们只需要第二行(用第二行求得CPU占用率,精确数据) 
 5     // 第一行:User 35%, System 13%, IOW 0%, IRQ 0% // CPU占用率 
 6     // 第二行:User 109 + Nice 0 + Sys 40 + Idle 156 + IOW 0 + IRQ 0 + SIRQ 1 = 306 
 7     // // CPU使用情况 
 8     public static synchronized String run(String[] cmd) { 
 9         String line = ""; 
10         InputStream is = null; 
11         try { 
12             Runtime runtime = Runtime.getRuntime(); 
13             Process proc = runtime.exec(cmd); 
14             is = proc.getInputStream(); 
15      
16             // 换成BufferedReader 
17             BufferedReader buf = new BufferedReader(new InputStreamReader(is)); 
18             do { 
19                 line = buf.readLine(); 
20                 // 前面有几个空行 
21                 if (line.startsWith("User")) { 
22                     // 读到第一行时,我们再读取下一行 
23                     line = buf.readLine(); 
24                     break; 
25                 } 
26             } while (true); 
27      
28             if (is != null) { 
29                 buf.close(); 
30                 is.close(); 
31             } 
32         } catch (IOException e) { 
33             e.printStackTrace(); 
34         } 
35         return line; 
36     } 
37      
38     // 获取指定应用的top命令获取的信息 
39     // PID CPU% S #THR VSS RSS PCY UID Name // 进程属性 
40     // 如果当前应用不在运行则返回null 
41     public static synchronized String run(String[] cmd, String pkgName) { 
42         String line = null; 
43         InputStream is = null; 
44         try { 
45             Runtime runtime = Runtime.getRuntime(); 
46             Process proc = runtime.exec(cmd); 
47             is = proc.getInputStream(); 
48      
49             // 换成BufferedReader 
50             BufferedReader buf = new BufferedReader(new InputStreamReader(is)); 
51             do { 
52                 line = buf.readLine(); 
53                 // 读取到相应pkgName跳出循环(或者未找到) 
54                 if (null == line || line.endsWith(pkgName)) { 
55                     break; 
56                 } 
57             } while (true); 
58      
59             if (is != null) { 
60                 buf.close(); 
61                 is.close(); 
62             } 
63         } catch (IOException e) { 
64             e.printStackTrace(); 
65         } 
66         return line; 
67     } 

三、后记
   这次相关的仅有的那个工程好像不能放上来了==。
 
   好吧,把我当时整理的一点点相关资料放附件了:包含《Android系统》文件夹和《深入研究java.lang.ProcessBuilder类.doc》。
   文件夹内容如下:

  

 ps:现在有个可以看的小工程了,请移至《Android VNC Server New》!

本文出自 “-_--___---_-” 博客,请务必保留此出处http://vaero.blog.51cto.com/4350852/778139

 附件: Android执行shell命令.zip

原文地址:https://www.cnblogs.com/eustoma/p/4358077.html