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 ...
随机推荐
- shell自动计算脚本
shell自动计算脚本 #!/bin/bash echo $(($)) [root@bogon ~]# sh b.sh 123+123246 let用户声明这个操作是要计算,后者的效率更高 (expr ...
- A股中为什么有涨幅超过10%的
在A股规定涨跌幅10%的正常交易日中,某些股票的涨跌幅却超出或没达到10%的,原因是因为A股的交易中最小的价格“申报单位为0.01元”,就是说某些股票不管什么价格都不会刚刚好是10%,它只能“取最近于 ...
- 微信分享JS接口失效说明及解决方案
关键字:微信分享 JS 失效 分享到朋友圈 微信分享JS接口目前已失效,以前可以自定义分享的标题.描述.图片.链接地址在微信6.0.2版本中失效. 官方回复如下: 旧版的获取分享状态及设置分享内容的 ...
- 跳到下个View
nextWebView = [[ WEBViewController alloc ] initWithNibName : @"WEBViewController" bundle : ...
- Janus WinForms Controls
http://blog.sina.com.cn/s/blog_68eb92020101kpw8.html
- cacti批量添加主机脚本
#!/bin/bash ##cacti批量脚本位置 device=/var/www/html/cacti/cli/add_device.php graphs=/var/www/html/cacti/c ...
- eclipse启动无响应,停留在Loading workbench状态
做开发的同学们或多或少的都会遇到eclipse启动到一定程度时,就进入灰色无响应状态再也不动了.启动画面始终停留在Loading workbench状态.反复重启,状态依旧. 多数情况下,应该是非正常 ...
- Java类加载器深入探索
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 什么是.class文件? class文件全名称为Java class文件,主要在平台无关性和网 ...
- composer很慢修改镜像
有两种方式启用本镜像服务: 系统全局配置: 即将配置信息添加到 Composer 的全局配置文件 config.json 中.见“例1” 单个项目配置: 将配置信息添加到某个项目的 composer. ...
- dlopen、dlsym和dlclose的使用
在dlopen()函数以指定模式打开指定的动态链接库文件,并返回一个句柄给dlsym()的调用进程.使用dlclose()来卸载打开的库. dlopen: dlopen() The function ...