Implementation of Android to Obtain Super Administrator Authority

  • 2021-11-24 02:53:55
  • OfStack

1. Define a special broadcast receiver, the broadcast receiver of the system super administrator


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

2. In the AndroidManifest. xml file, register the broadcast recipient of the super administrator


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

3. Create policy statement xml in res/xml


<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
 <force-lock/><!-- Forced screen locking -->
 <wipe-data/><!-- Clear data -->
 <reset-password/><!-- Reset password -->
 ...
</uses-policies>

Additional knowledge: Android gets ROOT permissions through code

Getting ROOT permission of Android is actually very simple, just execute the command "su" under Runtime.

First of all, we need to check whether we already have root permission. The judgment code is as follows:


//  Judge whether there is a ROOT Authority 
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;
}

Then we execute the code to get root permission


//  Get ROOT Authority 
public void get_root(){
 if (is_root()){
  Toast.makeText(mCtx, " Already has a ROOT Authority !", Toast.LENGTH_LONG).show();
 }
 else{
  try{
   progress_dialog = ProgressDialog.show(mCtx, 
     "ROOT", " Getting ROOT Authority ...", true, false);
   Runtime.getRuntime().exec("su");
  }
  catch (Exception e){
   Toast.makeText(mCtx, " Get ROOT Error while permissions !", Toast.LENGTH_LONG).show();
  }
 }

}


Related articles: