1. 程式人生 > >android中Root許可權的判斷與獲取

android中Root許可權的判斷與獲取

關於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;
    }