1. Create a broadcast, this get permission is a broadcast operation
<receiverAndroid:name= ". Activity. Deviceadminsample "Android:label= "@string/sample_device_admin"android:description= "@string/sample_device_admin_description"android:permission= "Android.permission.BIND_DEVICE_ADMIN"> <Meta-dataAndroid:name= "Android.app.device_admin"Android:resource= "@xml/device_admin_sample" />//This XML file is defined by what this super permission contains, and it needs to be created again under an XML folder to define <Intent-filter> <ActionAndroid:name= "Android.app.action.DEVICE_ADMIN_ENABLED" /> </Intent-filter> </receiver>
And then the definition of that @xml/device_admin_sample file
<Device-adminxmlns:android= "Http://schemas.android.com/apk/res/android"> <uses-policies> <Limit-password/> <Watch-login/> <Reset-password/> <Force-lock/> <Wipe-data/> <Expire-password/> <Encrypted-storage/> <Disable-camera/> </uses-policies></Device-admin>
The previous file is defined, and the following deviceadminsample is a broadcast class Deviceadminreceiver that inherits from a system definition, which gets super privilege through this broadcast.
Public classDeviceadminsampleextendsdeviceadminreceiver{voidShowtoast (context context, string msg) {string Status=context.getstring (R.string.admin_receiver_status, msg); Toast.maketext (context, status, Toast.length_short). Show (); } @Override Public voidonenabled (Context context, Intent Intent) {showtoast (context, context.getstring (R.string.admin_receiver_statu s_enabled)); } @Override Publiccharsequence ondisablerequested (context context, Intent Intent) {returncontext.getstring (r.string.admin_receiver_status_disable_warning); } @Override Public voidondisabled (Context context, Intent Intent) {showtoast (context, context.getstring (r.string.admin_receiver_stat us_disabled)); } @Override Public voidonpasswordchanged (Context context, Intent Intent) {showtoast (context, context.getstring (R.string.admin_receiv er_status_pw_changed)); }}
Gets the code for the permission by opening a Intent object that, through the system's Devicepolicymanager.action_add_device_admin intent, opens the interface that gets the system permission to let the user determine whether to grant these permissions
Public void activesuperuser (view view) {
ComponentName mdeviceadminsample = new ComponentName (this, deviceadminsample.class); That Deviceadminsample is the broadcast class we defined earlier.
New Intent (devicepolicymanager.action_add_device_admin); Intent.putextra (Devicepolicymanager.extra_device_admin, mdeviceadminsample); Mdeviceadminsample is Intent.putextra (devicepolicymanager.extra_add_explanation, r.string.add_admin_ Extra_app_text); Startactivityforresult (Intent, request_code_enable_admin); Turn on the activity that granted permissions }
The next step is to use the Devicepolicymanager to specifically manipulate the operations that require permissions after obtaining permissions.
Devicepolicymanager DPM = (Devicepolicymanager) Getsystemservice (Device_policy_service); Get System Services
if (dpm.isadminactive (mdeviceadminsample)) {//determines if the super-privilege has been acquired
Dpm.locknow (); Lock screen
}
Android Get Super Permissions