程序中如何获取Android的Root权限
16lz
2021-01-23
要在android应用程序中使用root权限,那么运行程序的设备必须具有root权限。
public static boolean runRootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.d(TAG, "the device is not rooted, error message: " + e.getMessage());
return false;
} finally {
try {
if (os != null) {
os.close();
}
if(process != null) {
process.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
public static boolean runRootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command+"\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.d(TAG, "the device is not rooted, error message: " + e.getMessage());
return false;
} finally {
try {
if (os != null) {
os.close();
}
if(process != null) {
process.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
如果设备获取了root权限,那么程序执行su命令时,就会提示用户进行授权.
点击允许,那么我们的应用程序就得到了root权限,可以继续往下执行。
前提:真机必须有root权限才能运行此方法,也就是你的机器要经过root破解才行。经过root破解后才会有su命令,我们就是通过su命令来获得管理员权限运行我们的程序的。
本文出自 “android” 博客,请务必保留此出处http://1622511.blog.51cto.com/1612511/567329
更多相关文章
- Android模仿乐淘的应用程序分析源码
- android应用程序组件之间的通讯
- android 学习五 设置应用程序全屏(没有状态栏和标题栏)
- 程序小白----AndroidStudio之飞机大战
- 怎样使用android自带例子程序
- Android程序结构分析
- Android应用程序组件Content Provider在应用程序之间共享数据的
- Android的权限分类