Android phones generally use the power button to lock the screen while turning off the screen light.
In fact, starting with API Level 8 (that is, Android 2.2), Android provides the Devicepolicymanager class that allows your application to perform screen lock operations as well.
Lock effect:
In order for your app to implement this screen lock, there are several main categories to use: Devicepolicymanager
This is the main class for device management. It can achieve screen lock, screen brightness adjustment, factory settings and other functions.
Deviceadminreceiver
This class inherits from Broadcastreceiver. From the source can be seen, in fact, is the implementation of a OnReceive method, the method according to different action, the corresponding operation. For example, if the activation succeeds, then action is action_device_admin_enabled, and the Onenabled method is called accordingly.
system source code:
/** * intercept standard device administrator broadcasts . implementations * should not override this method; it is better to implement the * convenience callbacks for each action. */ @Override public void onreceive (context context, intent Intent) { string action = intent.getaction ( ); if (Action_password_changed.equals (ACTION)) { onpasswordchanged (context, intent ); } else if (Action_password_failed.equals ( Action)) { onpasswordfailed (context, intent); } else if (Action_password_succeeded.equals (ACTION)) { onpasswordsucceeded (Context, intent); } else if (ACTION_DEVICE_ADMIN_ Enabled.equals (Action) { Onenabled (context, intent); } else if ( Action_device_admin_disable_requested.equals (Action) { charsequence res = ondisablerequested (context, intent); if (res != null) { &nbsP; bundle extras = getresultextras (True); Extras.putcharsequence (extra_disable_warning, res); } } else if (Action_device_ Admin_disabled.equals (Action) { Ondisabled (context, intent); } }
A simple reference in detail:
Screen lock and restore factory settings
1. The broadcast recipient who created the MyAdmin inherits Deviceadminreceiver
<receiver android:name= ". MyAdmin "> <meta-data android:name=" android.app.device_admin "android:resource=" @xml/my_adm In "/> <intent-filter> <action android:name=" Android.app.action.DEVICE_ADMIN_ENABLED "/> </intent-filter> </receiver>
My_admin.xml
<?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/> </us Es-policies></device-admin>
2. Register the broadcast recipient as the admin device
ComponentName madminname = new ComponentName (this, myadmin.class); if (mservice! = null) {if (!mservice.isadminacti ve (Madminname)) {Intent Intent = new Intent (devicepolicymanager.action_add_device_admin); Intent.putextra (Devicepolicymanager.extra_device_admin,madminname); StartActivity (Intent); }}
3. Get Idevicepolicymanager
Devicepolicymanager DM = (devicepolicymanager) getsystemservice (device_policy_service);p ublic void Lock (view view) { Dm.resetpassword ("123", 0); You need to enter the password (the new password in the method) to unlock the Dm.locknow () after swiping the screen bar. Restore factory settings. Dm.wipedata (0);
<uses-permission android:name= "Android.permission.RECEIVE_USER_PRESENT"/>
<uses-permission android:name= "Android.permission.WAKE_LOCK"/
Android Idevicepolicymanager for Manual lock screen