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. 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 = new byte[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 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. 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 Set Size 虚拟耗用内存(包含共享库占用的内存)
  24. RSS Resident Set Size 实际使用物理内存(包含共享库占用的内存)
  25. PCY OOXX,不知道什么东东
  26. UID 运行当前进程的用户id
  27. Name 程序名称android.process.media
  28. // ps:内存占用大小有如下规律:VSS >= RSS >= PSS >= USS
  29. // PSS Proportional Set Size 实际使用的物理内存(比例分配共享库占用的内存)
  30. // USS Unique Set Size 进程独自占用的物理内存(不包含共享库占用的内存)

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

2)执行代码
                        
  1. // top命令
  2. public static final 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. public static synchronized 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. public static synchronized 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. }

转自:http://vaero.blog.51cto.com/4350852/778139

更多相关文章

  1. Android(安卓)pm命令使用方法
  2. Android配置----小米手机通过wifi连接ADB调试Android应用
  3. Activity的属性
  4. Android震动实现
  5. Binder 与AIDL
  6. Android开发常用的linux命令、命令行操作、抓包等
  7. Android_adb详解
  8. Android修改触摸按键震动强弱
  9. AlarmManagerService(一)

随机推荐

  1. 环境搭建好后的第一个Android(安卓)项目
  2. ListView监听OnItemClick无响应
  3. 腾讯微博android授权 SharedPreferences
  4. Android把View转换成Bitmap
  5. Android(安卓)教你打造炫酷的ViewPagerIn
  6. Android(安卓)JNI直接调用驱动程序
  7. Android(安卓)ScrollView中包含EditText
  8. 在Eclipse中对Android应用签名
  9. Android(安卓)编译系统 (一)
  10. xml中Android的解析