Android 常用工具类之RuntimeUtil
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的更多相关文章
- 53. Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefer ...
- Android 常用工具类之SPUtil,可以修改默认sp文件的路径
参考: 1. 利用Java反射机制改变SharedPreferences存储路径 Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import ...
- 【转】Android常用工具类
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.Prefe ...
- android常用工具类
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkIn ...
- Android 常用工具类之 ScreenUtil
需求: 截屏 参考 : Android开发:截屏 screenshot 功能小结 package bvb.de.openadbwireless.utils; import android.app ...
- Android 常用工具类之LogUtil,可以定位到代码行,双击跳转
package cn.utils; import android.util.Log; public class LogUtils { public static boolean isDebug = t ...
- Android常用工具类封装---SharedPreferencesUtil
SharedPreferences常用于保存一些简单的数据,如记录用户操作的配置等,使用简单. public class SharedPreferencesUtil { // ...
- Android 常用工具类之 DimenUtil
public class DimenUtil { /** sp转换成px */ public static int sp2px(float spValue) { float fontScale = M ...
- Android 常用工具类之DeviceInfoUtil
public class DeviceInfoUtil { private static WifiManager wifiManager = null; // wifi是否已连接 public sta ...
随机推荐
- C# WebSocket 服务端示例代码 + HTML5客户端示例代码
WebSocket服务端 C#示例代码 using System; using System.Collections.Generic; using System.Linq; using System. ...
- C# --通过枚举获取系统颜色
public static System.Drawing.Color GetSystemColor(int i) { Array Colors = System.Enum.GetValues(type ...
- 第一篇 SQL Server安全概述
本篇文章是SQL Server安全系列的第一篇,详细内容请参考原文. Relational databases are used in an amazing variety of applicatio ...
- new操作符做了什么??
在javascript中,new操作符随处可见,我讲一下我自己对new操作符的理解... 构造函数无返回值 //测试代码 function Foo(name) { var age = 20; this ...
- asmdisk opened & asmdisk cached
ASMDISK OPENED - Disk is present in the storage system and is being accessed by Automatic Storage Ma ...
- 《30天自制操作系统》02_day_学习笔记
helloos3: helloos.nas的解释在P29中 接下来课本讲了一些汇编语言的知识,便于理解这个汇编文件helloos4: 讲解在P41 helloos.nas后半部分去掉就成了ipl.as ...
- 【java开发系列】—— JDK安装
前言 作为一个java开发者,安装JDK是不可避免的,但是配置路径却总是记不住,百度也有很多参考例子.这里仅仅当做以后参考的笔记记录. 说到JDK,就不得不提JRE.他们到底是什么呢? 通常我们进行j ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(6)
12.6 添加节点 Postgres-XC允许您在那个过程中的任何一个时间点添加新的服务器到计划中.所有您需要做的是按照我们之前演示的设置一个节点,并在 控制器上调用CREATE NODE.然后,该系 ...
- Groupon面经Prepare: Max Cycle Length
题目是遇到偶数/2,遇到奇数 *3 + 1的题目,然后找一个range内所有数字的max cycle length.对于一个数字,比如说44,按照题目的公式不停计算,过程是 44, 22, 11, 8 ...
- 转:python webdriver API 之设置等待时间
有时候为了保证脚本运行的稳定性,需要脚本中添加等待时间.sleep(): 设置固定休眠时间. python 的 time 包提供了休眠方法 sleep() , 导入 time 包后就可以使用 slee ...