Android 2.2 introduces an Android device management API that supports enterprise applications. The Device Management API provides device management functionality at the system level. Administrators can also remotely wipe (that is, restore factory defaults) lost or stolen phones.
However, to implement these features requires the introduction of device admin, specific 2, see document DEVELOP-->API Guides--->administration
Here we look at the implementation:
First create the MyAdmin class and inherit the Deviceadminreceiver
You don't have to implement anything.
Androidmanifest.xml Configure this receiver
<receiver android:name= "Com.example.lockscreen.MyAdmin" android:description= "@string/sample_device_admin_description" & nbsp android:label= "@string/sample_device_admin" android:permission= " Android.permission.BIND_DEVICE_ADMIN > <meta-data & nbsp android:name= "android.app.device_admin" &NBSP ; Android:resource= "@xml/device_admin_sample"/> <intent-filter> <action android:name= "android.app.action.DEVICE_ADMIN_ ENABLED "/> </intent-filter> </RECEIVER&G T
Name fill in their own classpath, at this time sample_device_admin and so will be error, we need to explain in string.xml
<string name= "Sample_device_admin_description" > I am the device Super Administrator, I can lock the screen after opening. </string> <string name= "Sample_device_admin" > One-Touch lock screen settings </string>
Java Main method implementation
Declare Devicepolicymanager first, package name import Android.app.admin.DevicePolicyManager; Real code as an example
Package Com.example.lockscreen;import Android.app.activity;import Android.app.admin.devicepolicymanager;import Android.content.componentname;import Android.content.intent;import Android.net.uri;import Android.os.Bundle; Import Android.view.view;import Android.widget.toast;public class Mainactivity extends Activity {private Devicepolicymanager DPM; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);dp m = (Devicepolicymanager) getsystemservice (DEVICE_ Policy_service);} /** * Turn on device administrator * @param view */public void Openadmin (view view) {//Declare an intention to turn on the device's Super admin Intent Intent = new Intent (devicep Olicymanager.action_add_device_admin); ComponentName cn = New ComponentName (this, myadmin.class); Intent.putextra (Devicepolicymanager.extra_device_admin, CN); Persuade the user to turn on admin intent.putextra (devicepolicymanager.extra_add_explanation, "Turn on me. Open I will be able to lock the screen, turn on send points "); startactivity (intENT);} /** * Lock Screen * @param view */public void Click (View view) {ComponentName cn = new ComponentName (this, myadmin.class); if (Dpm.isa Dminactive (CN)) {///device Administrator Apidpm.resetpassword ("123", 0);DP M.locknow ();//dpm.wipedata (0);//dpm.wipedata ( Devicepolicymanager.wipe_external_storage);//delete sdcard data}else{//openadmin (NULL); Toast.maketext (This, "Please activate Administrator First", 0). Show ();}} /** * Uninstall * @param view */public void Uninstall (view view) {ComponentName cn = new ComponentName (this, myadmin.class);//Can be moved Except Administrator Dpm.removeactiveadmin (CN); Intent Intent = new Intent (); Intent.setaction ("Android.intent.action.UNINSTALL_ Package "), Intent.addcategory (" Android.intent.category.DEFAULT "), Intent.setdata (Uri.parse (" Package: "+ Getpackagename ())); StartActivity (intent);}}
Finally, the <button's OnClick property configuration method name in the foreground XML can be called.
<button android:onclick= "openadmin" android:layout_alignparenttop= "true" android:layout_width= " Wrap_content " android:layout_height=" wrap_content " android:text=" turn on Super admin "/> <button android:onclick= "click" android:layout_centerinparent= "true" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "one button lock screen"/> <button android:onclick= " Uninstall " android:layout_alignparentbottom=" true " android:layout_width=" Wrap_content " android: layout_height= "Wrap_content" android:text= "unload"/>
Android One-click Lock screen and clear data code implementation