android中Root权限的判断与获取

    xiaoxiao2025-08-06  5

    关于android中root权限的相关判断与获取,我咋这里做一下笔记。

    首先是判断手机是否有root权限:

    /** * 判断当前手机是否有ROOT权限 * @return */ public static boolean isRoot(){ boolean bool = false; try{ if ((!new File("/system/bin/su").exists()) && (!new File("/system/xbin/su").exists())){ bool = false; } else { bool = true; } } catch (Exception e) { } return bool; }

    获取Root权限:

    /** * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限) * @param command 命令:String apkRoot="chmod 777 "+getPackageCodePath(); RootCommand(apkRoot); * @return 应用程序是/否获取Root权限 */ public static boolean RootCommand(String command) { Process process = null; DataOutputStream os = null; try { process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream()); os.writeBytes(command + "\n"); os.flush(); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception e) { Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage()); return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { } } Log.d("*** DEBUG ***", "Root SUC "); return true; }

    转载请注明原文地址: https://ju.6miu.com/read-1301480.html
    最新回复(0)