Generally, when an Android phone is not used, it uses the power key to lock the screen and turn off the screen light.
In fact, from API Level 8 (Android 2.2), Android provides the devicepolicymanager class, allowing your application to perform screen lock and other operations.
Lock effect:
To lock your application to the screen, you need to use the following categories: devicepolicymanager
This is the main category of device management. It enables screen locking, screen brightness adjustment, factory settings, and other functions.
Deviceadminreceiver
This class inherits from broadcastreceiver. From the source code, we can see that an onreceive method is actually implemented. This method performs corresponding operations based on different actions. For example, if activation is successful, action is action_device_admin_enabled, And the onenabled method is called accordingly.
System source code:
- /**
- * Intercept standard device administrator broadcasts. implementations
- * Shocould not override this method; it is better to implement
- * Convenience callbacks for each action.
- */
- @ Override
- Public void onreceive (context, 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 ){
- Bundle extras = getresultextras (true );
- Extras. putcharsequence (extra_disable_warning, Res );
- }
- } Else if (action_device_admin_disabled.equals (Action )){
- Ondisabled (context, intent );
- }
- }
Simple reference:
Screen lock and restore factory settings
1. Create a broadcast receiver for MyAdmin to inherit deviceadminreceiver
<Cycler 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>
</Cycler>
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/>
</Uses-policies>
</Device-Admin>
2. Get idevicepolicymanager
Devicepolicymanager dm = (devicepolicymanager) getsystemservice (device_policy_service );
3. register the broadcast receiver as the admin device.
Componentname madminname = new componentname (this, MyAdmin. Class );
If (mservice! = NULL ){
If (! Mservice. isadminactive (madminname )){
Intent intent = new intent (devicepolicymanager. action_add_device_admin );
Intent. putextra (devicepolicymanager. extra_device_admin, madminname );
Startactivity (intent );
}
}
4. Lock screen or clear data processing in click events
Public void lock (view ){
DM. resetpassword ("123", 0); // After sliding the unlock screen bar, enter the password (the new password in the method) to unlock the screen.
DM. locknow ();
// Restore the factory settings.
// DM. wipedata (0 );
}