Upgrade the application to have system administrator privileges. Write a broadcast receiver, give the broadcast recipient to apply for system administrator permissions, let the operating system to the broadcast recipient authorization. (Activates the authorization component of the system). In this, I mainly by applying for application system administrator privileges, lock screen, set password, restore factory settings a few aspects to simple introduction to Android in how to apply to an application system administrator permissions operation. These are the main features of the android provided by a class to complete, this class is Devicepolicymanager, the detailed description of this class, you can refer to the official Android document, here, I will not be too much elaboration. Now, let's take a look at how to implement these features.
1. Create a My_admin.xml file
Create a new XML directory under the Res directory of the Android project and create a new My_admin.xml file in the XML directory
The specific implementation is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><device-admin xmlns:android= "http://schemas.android.com/apk/res/ Android > <uses-policies> <limit-password/> <watch-login/> < Reset-password/> <force-lock/> <wipe-data/> </uses-policies></device-admin >
This file mainly defines some of the system administrator permissions to get
2. Create a layout file
This layout file has a button that implements the function in the OnClick event of the button
The specific implementation is as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Mainactivity "> <button android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:onclick=" OnClick " android:text=" lock screen/></relativelayout>
3. Define the broadcast receiver MyAdmin
This class is different from the ordinary broadcast receiver, it needs to inherit Deviceadminreceiver, the detailed explanation about Deviceadminreceiver, you can refer to the official Android document.
The specific implementation is as follows
Package Com.lyz.local.screen.activity;import android.app.admin.deviceadminreceiver;/** * Broadcast recipient * @author Liuyazhuang * */public class MyAdmin extends Deviceadminreceiver {}
4, Perfect mainactivity
In this class, the main implementation of the page button click event, in this click event, through the Devicepolicymanager class to achieve the corresponding operation
The specific code is as follows:
Package Com.lyz.local.screen.activity;import Android.os.bundle;import Android.app.activity;import Android.app.admin.devicepolicymanager;import Android.content.componentname;import Android.content.Context;import Android.content.intent;import Android.view.menu;import android.view.view;/** * Program Entry * @author Liuyazhuang * */public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} public void OnClick (View v) {//Get System administrative rights devicepolicymanager Devicepolicymanager = (Devicepolicymanager) This.getsystemservice (Context.device_policy_service);//request permission ComponentName componentname = new ComponentName (This, Myadmin.class);//Determine if the component has System administrator permissions Boolean isadminactive = Devicepolicymanager.isadminactive (componeNtname), if (isadminactive) {//Lock screen Devicepolicymanager.locknow ();//Reset Password Devicepolicymanager.resetpassword ("123", 0); /factory Reset (not recommended for real-machine testing) devicepolicymanager.wipedata (0);} Else{intent Intent = new Intent ();//Specify Action intent.setaction (devicepolicymanager.action_add_device_admin);// Which component is assigned to authorize Intent.putextra (Devicepolicymanager.extra_device_admin, componentname); startactivity (intent);}}
5, Registered Broadreceiver
Finally, don't forget to register in Androidmanifest.xml Broadreceiver
The specific implementation is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.lyz.local.screen.activity "android:versioncode=" 1 "android:versionname=" 1.0 "> <us ES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allow" Backup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@s Tyle/apptheme "> <activity android:name=" com.lyz.local.screen.activity.MainActivity "an Droid:label= "@string/app_name" > <intent-filter> <action android:name= "Android.inten T.action.main "/> <category android:name=" Android.intent.category.LAUNCHER "/> </int ent-filter> </activity> <receiver android:name= ". MyAdmin "><meta-data android:name=" Android.app.device_admin "Android:resouRce= "@xml/my_admin"/><intent-filter><action android:name= "Android.app.action.DEVICE_ADMIN_ENABLED"/ ></intent-filter></receiver> </application></manifest>
6. Operation Effect
1. Show app authorized administrator rights
2. Click Lock Screen
3. Enter the password
4. The problem that the uninstall application appears solves the method
Warm tip: You can go to link http://download.csdn.net/detail/l1028386804/8924361 to get the full Android sample code
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-Application System administrator privileges