要在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命令时,就会提示用户进行授权,如图:


点击允许,那么我们的应用程序就得到了root权限,可以继续往下执行。

前提:真机必须有root权限才能运行此方法,也就是你的机器要经过root破解才行。经过root破解后才会有su命令,我们就是通过su命令来获得管理员权限运行我们的程序的。

更多相关文章

  1. Android(安卓)将文件保存到SD卡中
  2. android 读取sd卡中的图片
  3. 运行时改变Button的android:drawableTop
  4. android manifest.xml中元素含义
  5. Android动态获取定位权限(包括Android10.0)
  6. 如何消除原生Android原生网络连接显示x或者惊叹号
  7. android系统权限大全
  8. 使用logcat命令
  9. Android应用程序获取ROOT权限的方法 (基础篇)

随机推荐

  1. android Immutable bitmap passed to Can
  2. Android(安卓)文件操作
  3. android:onClick --new attribute from S
  4. Android中替换ExpandableListView控件前
  5. Android涂鸦简单制作
  6. 模拟器无法启动,卡在android字样的界面上
  7. android按钮的操作例子,简单大家看明白_基
  8. android开发环境之ADT安装,卸载,更新
  9. 安卓模拟器设置网速和延迟
  10. android 文件上传