程序中如何获取Android的Root权限
16lz
2021-12-04
要在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;
}
如果设备获取了root权限,那么程序执行su命令时,就会提示用户进行授权,如图:
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(安卓)将文件保存到SD卡中
- android 读取sd卡中的图片
- 运行时改变Button的android:drawableTop
- android manifest.xml中元素含义
- Android动态获取定位权限(包括Android10.0)
- 如何消除原生Android原生网络连接显示x或者惊叹号
- android系统权限大全
- 使用logcat命令
- Android应用程序获取ROOT权限的方法 (基础篇)