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 ...
随机推荐
- 注册表操作命令和自定义cmd窗口
REM @echo offREM clsREM echo Microsoft Windows 7REM echo ------------------------REM echo Welcome to ...
- jq的核心基础
1.jq代码的风格 jq对象$().css();jQuery().css(); //获取jquery对象 示例$().css('background':'red').css('font-siz ...
- Python开发【第二章】:Python的数据类型
基本数据类型 一.整型 如: 18.73.84 整型具备如下功能: class int(object): """ int(x=0) -> int or long i ...
- Android 使用PullToRefresh实现下拉刷新和上拉加载(ExpandableListView)
PullToRefresh是一套实现非常好的下拉刷新库,它支持: 1.ListView 2.ExpandableListView 3.GridView 4.WebView 等多种常用的需要刷新的Vie ...
- window.open下载文件ie8请求的站点不可用的解决办法
在业务里用到了PHP header导出doc文档,GET传值到页面,读出相应数据输出doc文件下载.用户提出需要批量,于是设计成js循环出对应数量的window.open(),向页面传入不同的值,批量 ...
- Hadoop学习(4)-- MapReduce
MapReduce是一种用于大规模数据集的并行计算编程模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题.其主要思想Map(映射)和Reduce(规约)都是从函数是编程语言中借鉴而来的 ...
- MVC ViewModel
ViewModel这个概念不只是在在MVC模式中有,你会在很多关于MVC.MVP.MVVM的文章中见到这个说法,并且这个概念在任何技术中都有可能提到,比如ASP.NET, Silverlight, W ...
- Java基础之线程——管理线程同步方法(BankOperation2)
控制台程序. 当两个或多个线程共享同一资源时,例如文件或内存块,就需要采取措施,确保其中的一个线程不会修改另一个线程正在使用的资源.当其中的一个线程更新文件中的某个记录,同时另一个线程正在检索这个记录 ...
- mapreduce小结
(不断更新) MapReduce架构是一种分布式编程架构,它本质上是将任务划分,然后归并.它是以数据为中心的编程架构,相比与分布式计算和并行计算等,它更看重的是吞吐率.它处理的数据是PB级的数据,它并 ...
- nodejs发起HTTPS请求并获取数据
摘要:在网站中有时候需要跨域请求数据,直接用Ajax无法实现跨域,采用其他方式需要根据不同的浏览器做相应的处理.用Nodejs可以很好的解决这些问题,后台引用HTTPS模块,发送和返回的数据均为JSO ...