android - 如何在 Android 9 应用程序中授予超级用户访问权限
问题描述
我试图以超级用户身份从 Android 应用程序级别访问。使用运行 SDK-28 的根设备
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
我试过这个,但我知道它在 Android-6 之后不再支持。还有其他方法吗?
进程进程 = Runtime.getRuntime().exec("su");
它显示没有权限执行“su”
解决方案
您需要做的就是扩展此类并覆盖 getCommandsToExecute 方法以返回您想要以 root 身份执行的命令。
检查链接以获得更多帮助: http: //muzikant-android.blogspot.com/2011/02/how-to-get-root-access-and-execute.html
public abstract class ExecuteAsRootBase
{
public static boolean canRunRootCommands()
{
boolean retval = false;
Process suProcess;
try
{
suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
DataInputStream osRes = new DataInputStream(suProcess.getInputStream());
if (null != os && null != osRes)
{
// Getting the id of the current user to check if this is root
os.writeBytes("id\n");
os.flush();
String currUid = osRes.readLine();
boolean exitSu = false;
if (null == currUid)
{
retval = false;
exitSu = false;
Log.d("ROOT", "Can't get root access or denied by user");
}
else if (true == currUid.contains("uid=0"))
{
retval = true;
exitSu = true;
Log.d("ROOT", "Root access granted");
}
else
{
retval = false;
exitSu = true;
Log.d("ROOT", "Root access rejected: " + currUid);
}
if (exitSu)
{
os.writeBytes("exit\n");
os.flush();
}
}
}
catch (Exception e)
{
// Can't get root !
// Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted
retval = false;
Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
}
return retval;
}
public final boolean execute()
{
boolean retval = false;
try
{
ArrayList<String> commands = getCommandsToExecute();
if (null != commands && commands.size() > 0)
{
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
// Execute commands that require root access
for (String currCommand : commands)
{
os.writeBytes(currCommand + "\n");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
try
{
int suProcessRetval = suProcess.waitFor();
if (255 != suProcessRetval)
{
// Root access granted
retval = true;
}
else
{
// Root access denied
retval = false;
}
}
catch (Exception ex)
{
Log.e("ROOT", "Error executing root action", ex);
}
}
}
catch (IOException ex)
{
Log.w("ROOT", "Can't get root access", ex);
}
catch (SecurityException ex)
{
Log.w("ROOT", "Can't get root access", ex);
}
catch (Exception ex)
{
Log.w("ROOT", "Error executing internal operation", ex);
}
return retval;
}
protected abstract ArrayList<String> getCommandsToExecute();
}
推荐阅读
- shell - 使用管道作为代码在jenkins master上找不到返回的shell脚本
- java - 如何在spring security中实现多个http请求
- time-series - Can I use a non-Date type as the time index of a TimeSeries object in Julia?
- python - How to group items into buckets of 1-10?
- python - PyAutoGUI,停止命令
- r - 我应该将 R 包 BNlearn 用于我的数据的交叉验证中的什么损失函数?
- kotlin - 如果为空,如何将新列表分配给可为空的字段,或者只是将元素添加到 Kotlin 中的现有列表?
- reactjs - 带有 NextJs 的 ModuleParseError
- ansible - 在 Ansible 中覆盖默认变量的属性
- java - Android:手机重启后设置闹钟/提醒