1. public class RuntimeUtil {
  2.  
  3. /** 通过查询su文件的方式判断手机是否root */
  4. public static boolean hasRootedSilent() {
  5. return new File("/system/bin/su").exists()
  6. || new File("/system/xbin/su").exists()
  7. || new File("/system/sbin/su").exists()
  8. || new File("/sbin/su").exists()
  9. || new File("/vendor/bin/su").exists();
  10. }
  11.  
  12. /** 通过执行命令的方式判断手机是否root, 会有申请root权限的对话框出现 */
  13. public static boolean hasRooted() {
  14. return execSilent("echo test");
  15. }
  16.  
  17. /** 执行命令获取结果集 */
  18. public static List<String> exec(String cmd) {
  19. List<String> dataList = null;
  20. BufferedWriter writer = null;
  21. BufferedReader reader = null;
  22. Process process = null;
  23. try {
  24. process = Runtime.getRuntime().exec("su");
  25. writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
  26. reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  27. runCmd(writer, cmd);
  28. process.waitFor();
  29. dataList = new ArrayList<>();
  30. String content;
  31. while (null != (content = reader.readLine())) {
  32. dataList.add(content);
  33. }
  34. } catch (Exception e) {
  35. //e.printStackTrace();
  36. } finally {
  37. closeCloseable(reader, writer);
  38. if (process != null) process.destroy();
  39. }
  40. return dataList;
  41. }
  42.  
  43. /** 执行一组命令 */
  44. public static void execSilent(String... cmd) {
  45. BufferedWriter writer = null;
  46. Process process = null;
  47. try {
  48. process = Runtime.getRuntime().exec("su");
  49. writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
  50. runCmd(writer, cmd);
  51. process.waitFor();
  52. } catch (Exception e) {
  53. // e.printStackTrace();
  54. } finally {
  55. closeCloseable(writer);
  56. if (process != null) process.destroy();
  57. }
  58. }
  59.  
  60. /** 判断进程是否正在运行 */
  61. public static boolean isProcessRunning(String processName) {
  62. List<String> processList = exec("ps");
  63. for (int i = 1; i < processList.size(); i++) {
  64. if (processList.get(i).endsWith(processName)) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70.  
  71. /** 判断是否成功执行 */
  72. public static boolean execSilent(String cmd) {
  73. boolean result = false;
  74. BufferedWriter writer = null;
  75. Process process = null;
  76. try {
  77. process = Runtime.getRuntime().exec("su");
  78. writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
  79. runCmd(writer, cmd);
  80. process.waitFor();
  81. Log.d("runtime", "onCreate: process.exitValue() " + process.exitValue());
  82. result = process.exitValue() == 0;
  83. } catch (Exception e) {
  84. // e.printStackTrace();
  85. } finally {
  86. closeCloseable(writer);
  87. if (process != null) process.destroy();
  88. }
  89. return result;
  90. }
  91.  
  92. // 关闭流文件
  93. private static void closeCloseable(Closeable... closeable) {
  94. for (int i = 0; i < closeable.length; i++) {
  95. if (null != closeable) {
  96. try {
  97. closeable[i].close();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. }
  103. }
  104.  
  105. // 执行命令
  106. private static void runCmd(BufferedWriter writer, String... cmd) throws IOException {
  107. for (int i = 0; i < cmd.length; i++) {
  108. writer.write(cmd[i] + "\n");
  109. writer.flush();
  110. }
  111. writer.write("exit \n");
  112. writer.flush();
  113. }
  114. }

Android 常用工具类之RuntimeUtil的更多相关文章

  1. 53. Android常用工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...

  2. Android 常用工具类之SPUtil,可以修改默认sp文件的路径

    参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...

  3. 【转】Android常用工具类

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefe ...

  4. android常用工具类

    import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkIn ...

  5. Android 常用工具类之 ScreenUtil

    需求: 截屏 参考 :    Android开发:截屏 screenshot 功能小结 package bvb.de.openadbwireless.utils; import android.app ...

  6. Android 常用工具类之LogUtil,可以定位到代码行,双击跳转

    package cn.utils; import android.util.Log; public class LogUtils { public static boolean isDebug = t ...

  7. Android常用工具类封装---SharedPreferencesUtil

    SharedPreferences常用于保存一些简单的数据,如记录用户操作的配置等,使用简单. public class SharedPreferencesUtil {              // ...

  8. Android 常用工具类之 DimenUtil

    public class DimenUtil { /** sp转换成px */ public static int sp2px(float spValue) { float fontScale = M ...

  9. Android 常用工具类之DeviceInfoUtil

    public class DeviceInfoUtil { private static WifiManager wifiManager = null; // wifi是否已连接 public sta ...

随机推荐

  1. Linux下获取公网IP地址

    curl http://members.3322.org/dyndns/getipcurl http://ip.6655.com/ip.aspx

  2. python中几个常用的算术函数

    1.lambda函数(匿名函数) lambda函数使用方式:lambda[参数1,参数2....]:表达式,列表 实例如下: lambda x : x * 2,[1,2,3,4] lambda 2.r ...

  3. centos linux安全和调优 第四十一节课

    centos  linux安全和调优    第四十一节课 上半节课 Linux安全 下半节课 Linux调优 2015-07-01linux安全和调优 [复制链接]--http://www.apele ...

  4. linux 文件目录

  5. SwipeRefreshLayout实现上拉加载下拉刷新

    package com.example.swiperefreshlayoutdemo; import java.util.ArrayList;import java.util.HashMap; imp ...

  6. 面向对象编程(九)——面向对象三大特性之继承以及重写、Object类的介绍

    面向对象三大特性 面向对象三大特征:继承 :封装/隐藏 :多态(为了适应需求的多种变化,使代码变得更加通用!) 封装:主要实现了隐藏细节,对用户提供访问接口,无需关心方法的具体实现. 继承:很好的实现 ...

  7. IntelliJ IDEA 的 Java 热部署插件 JRebel 安装及使用

    JRebel 介绍 JRebel for Intellij JRebel 在 Java Web 开发中, 一般更新了 Java 文件后要手动重启 Tomcat 服务器, 才能生效,  自从有了 JRe ...

  8. string函数分析

    string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把 ...

  9. Uint8Array 对象

    8 位无符号整数值的类型化数组.内容将初始化为 0.如果无法分配请求数目的字节,则将引发异常. 语法 uint8Array = new Uint8Array( length ); uint8Array ...

  10. java web工程读取及修改配置文件

    这篇博客比自己讲解的详细: http://blog.sina.com.cn/s/blog_69398ed9010191jg.html 使用方法: 1)配置文件在web-info的class目录下,或者 ...