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 ...
随机推荐
- ucenter 整合同步登录的内部实现原理及thinkphp整合ucenter
1.用户登录discuz,通过logging.php文件中的函数uc_user_login对post过来的数据进行验证,也就是对username和password进行验证.2.如果验证成功,将调用位于 ...
- Ubuntu下如何将普通用户提升到root权限
在ubuntu的系统操作中,我们经常会使用到系统权限的,因为权限不足,导致在一些操作当中非常麻烦.要获取权限,最长使用的方法就是使用sudo指令,但是来回使用还是比较麻烦,有没有直接提升我们的用户权限 ...
- [Slimdx]顶点和索引缓冲,绘制了2个分离的三角形
定义网格顶点和索引缓冲,绘制了2个分离的三角形. using System; using System.Drawing; using RGeos.SlimScene.Core; using SlimD ...
- 离线下载Windows 调试符号 Symbols
公司开发机没有不能连接到互联网.调试程序时那些Windows模块(如ntdll.dll)不能加载符号,而程序总是崩在这些模块里.想看一眼到底崩在了什么地方. 需要把对应的符号下载下来. 使用工具sym ...
- 查看python的路径
>>> import sys >>> sys.path
- .NET Framework 4 中的并行编程9---线程安全集合类
原文转载自:http://www.cnblogs.com/xray2005/archive/2011/10/11/2206745.html 在.Net 4中,新增System.Collections. ...
- struts标签小记
1.<s:iterator>标签的 奇偶数行使用不同样式 <s:iterator id="list" value="#request.listq&qu ...
- break和continue的区别以及标签label的使用
break表示直接跳出当前循环,break只能运用于switch--case语句以及循环之中 continue则表示跳出当次循环,继续执行下一次循环 label标签则可以选择break,或者conti ...
- 转:python webdriver API 之浏览器的操作
1.1.浏览器最大化在统一的浏览器大小下运行用例,可以比较容易的跟一些基于图像比对的工具进行结合,提升测试的灵活性及普遍适用性.比如可以跟 sikuli 结合,使用 sikuli 操作 flash.# ...
- MFC主窗口架构模型
根据主窗口类型,MFC软件工程可以分为一下几种架构模型: 1.SDI(Simple Document Interface)单文档界面,一个主窗口下只编辑一份文档 2.MDI(Multiple Docu ...