SoFunction
Updated on 2025-03-11

Implementation of Android to obtain super administrator permissions

1. Define special broadcast receivers, broadcast receivers of the system super administrator

public class MyDeviceAdminReceiver extends DeviceAdminReceiver{
 @Override
 public void onReceive(Context context,Intent intent){
  //TODO
 }
}

2. In the file, register the broadcast receiver of the super administrator

<receiver
 android:name=""
 android:permission=".BIND_DEVICE_ADMIN">
 <meta-data
  android:name=".device_admin"
  android:resource="@xml/device_admin_sample"/>
 <intent-filter>
  <action android:name=".DEVICE_ADMIN_ENABLED"/>
 </intent-filter>
</receiver>

3. Create a policy declaration in res/xml

&lt;device-admin xmlns:andro&gt;
&lt;uses-policies&gt;
 &lt;force-lock/&gt;&lt;!--Forced screen lock--&gt;
 &lt;wipe-data/&gt;&lt;!--Clear data--&gt;
 &lt;reset-password/&gt;&lt;!--Reset password--&gt;
 ...
&lt;/uses-policies&gt;

Supplementary knowledge:Android obtains ROOT permissions through code

Getting Android's ROOT permissions is actually very simple. Just execute the command "su" under Runtime.

First, we need to check whether we already have root permissions, and the judgment code is as follows:

// Determine whether you have ROOT permissionspublic static boolean is_root(){
 boolean res = false;
 try{ 
  if ((!new File("/system/bin/su").exists()) &amp;&amp; 
   (!new File("/system/xbin/su").exists())){
  res = false;
 } 
 else {
  res = true;
 };
 } 
 catch (Exception e) { 
 
 } 
 return res;
}

Then we execute the code to get root permissions

// Get ROOT permissionspublic void get_root(){
 if (is_root()){
  (mCtx, "Already have ROOT permission!", Toast.LENGTH_LONG).show();
 }
 else{
  try{
   progress_dialog = (mCtx, 
     "ROOT", "Getting ROOT permissions...", true, false);
   ().exec("su");
  }
  catch (Exception e){
   (mCtx, "An error occurred while obtaining ROOT permission!", Toast.LENGTH_LONG).show();
  }
 }

}

The above article on Android's acquisition of super administrator rights is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.