public class RuntimeUtil {

    /** 通过查询su文件的方式判断手机是否root */
public static boolean hasRootedSilent() {
return new File("/system/bin/su").exists()
|| new File("/system/xbin/su").exists()
|| new File("/system/sbin/su").exists()
|| new File("/sbin/su").exists()
|| new File("/vendor/bin/su").exists();
} /** 通过执行命令的方式判断手机是否root, 会有申请root权限的对话框出现 */
public static boolean hasRooted() {
return execSilent("echo test");
} /** 执行命令获取结果集 */
public static List<String> exec(String cmd) {
List<String> dataList = null;
BufferedWriter writer = null;
BufferedReader reader = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
runCmd(writer, cmd);
process.waitFor();
dataList = new ArrayList<>();
String content;
while (null != (content = reader.readLine())) {
dataList.add(content);
}
} catch (Exception e) {
//e.printStackTrace();
} finally {
closeCloseable(reader, writer);
if (process != null) process.destroy();
}
return dataList;
} /** 执行一组命令 */
public static void execSilent(String... cmd) {
BufferedWriter writer = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
runCmd(writer, cmd);
process.waitFor();
} catch (Exception e) {
// e.printStackTrace();
} finally {
closeCloseable(writer);
if (process != null) process.destroy();
}
} /** 判断进程是否正在运行 */
public static boolean isProcessRunning(String processName) {
List<String> processList = exec("ps");
for (int i = 1; i < processList.size(); i++) {
if (processList.get(i).endsWith(processName)) {
return true;
}
}
return false;
} /** 判断是否成功执行 */
public static boolean execSilent(String cmd) {
boolean result = false;
BufferedWriter writer = null;
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
runCmd(writer, cmd);
process.waitFor();
Log.d("runtime", "onCreate: process.exitValue() " + process.exitValue());
result = process.exitValue() == 0;
} catch (Exception e) {
// e.printStackTrace();
} finally {
closeCloseable(writer);
if (process != null) process.destroy();
}
return result;
} // 关闭流文件
private static void closeCloseable(Closeable... closeable) {
for (int i = 0; i < closeable.length; i++) {
if (null != closeable) {
try {
closeable[i].close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} // 执行命令
private static void runCmd(BufferedWriter writer, String... cmd) throws IOException {
for (int i = 0; i < cmd.length; i++) {
writer.write(cmd[i] + "\n");
writer.flush();
}
writer.write("exit \n");
writer.flush();
}
}

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. ionic 报错%1 is not a valid Win32 application

    Fixed the problem by installing python version 3.0 and above will do下载Python3.0或以上版本 python官网传送门:htt ...

  2. jenkins自动构建截图

  3. Android 多线程处理之多线程用法

    handler.post(r)其实这样并不会新起线程,只是执行的runnable里的run()方法,却没有执行start()方法,所以runnable走的还是UI线程. 1.如果像这样,是可以操作ui ...

  4. Ionic学习笔记四 一些问题处理

    版权声明:本文为博主原创文章,转载请留链接,非常感谢.   目录(?)[+]   IONIC actionsheet 的cancel menu在android下不显示的bug 在 _action-sh ...

  5. linux敲入命令不记录的方法

         history  查看命令列表  history                当前shell进程下的记录的                 每个伪终端 窗口都是一个单独的进程,会自己记录命 ...

  6. 发现EF中字段错误

    在使用EF时,报错: 对一个或多个实体的验证失败.有关详细信息,请参见“EntityValidationErrors”属性 添加一个验证方法: 代码: using System; using Syst ...

  7. linux input输入子系统应用分析

    输入设备(如按键.键盘.触摸屏.鼠标等)是典型的字符设备,其一般的工作机理是底层在按键.触摸等动作发送时产生一个中断(或驱动通过timer定时查询),然后CPU通过SPI.I2 C或外部存储器总线读取 ...

  8. eclipse中运行Selenium遇到的问题

    1.   java.lang.NoClassDefFoundError: 解决方法:eclipse的java工程中导入selenium-java-2.44.0\selenium-2.44.0\libs ...

  9. Tigase Server Clustering

    首先,在服务器上启用集群 修改init.properties --cluster-mode=true 自定义端口 允许自定义,但是所有的实例都要使用相同的端口,以便通讯 --cl-comp-ports ...

  10. Private strand flush not complete

    当切换日志的时候,所有private strands的内容都会被flush到当前的日志中,然后日志切换才可以完成. strand是在oracle 10g中引入的新术语,和redo的latches相关. ...