Android gets the instance code for the ROOT permission

  • 2020-05-19 05:43:54
  • OfStack

Getting the ROOT permission for Android is as simple as executing the command "su" under Runtime.


//  To obtain ROOT permissions 
public void get_root(){

    if (is_root()){
        Toast.makeText(mCtx, " Already have ROOT permissions !", Toast.LENGTH_LONG).show();
    }
    else{
        try{
            progress_dialog = ProgressDialog.show(mCtx, 
                    "ROOT", " Is access to ROOT permissions ...", true, false);
            Runtime.getRuntime().exec("su");
        }
        catch (Exception e){
            Toast.makeText(mCtx, " To obtain ROOT Error while permissions !", Toast.LENGTH_LONG).show();
        }
    }

}

Where is_root() determines whether you already have the ROOT permission. As long as one of the files /system/bin/su, /system/xbin/su exists, it indicates that it already has ROOT permissions. If neither exists, it does not have ROOT permissions.


//  To determine whether or not ROOT permissions 
public static boolean is_root(){

    boolean res = false;

    try{ 
        if ((!new File("/system/bin/su").exists()) && 
            (!new File("/system/xbin/su").exists())){
        res = false;
    } 
    else {
        res = true;
    };
    } 
    catch (Exception e) {  

    } 
    return res;


Related articles: