Android activates deviceadmin, locks the screen, and restores factory settings.

Source: Internet
Author: User
Tags myadmin

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:

  1. /**
  2. * Intercept standard device administrator broadcasts. implementations
  3. * Shocould not override this method; it is better to implement
  4. * Convenience callbacks for each action.
  5. */
  6. @ Override
  7. Public void onreceive (context, intent ){
  8. String action = intent. getaction ();
  9. If (action_password_changed.equals (Action )){
  10. Onpasswordchanged (context, intent );
  11. } Else if (action_password_failed.equals (Action )){
  12. Onpasswordfailed (context, intent );
  13. } Else if (action_password_succeeded.equals (Action )){
  14. Onpasswordsucceeded (context, intent );
  15. } Else if (action_device_admin_enabled.equals (Action )){
  16. Onenabled (context, intent );
  17. } Else if (action_device_admin_disable_requested.equals (Action )){
  18. Charsequence res = ondisablerequested (context, intent );
  19. If (res! = NULL ){
  20. Bundle extras = getresultextras (true );
  21. Extras. putcharsequence (extra_disable_warning, Res );
  22. }
  23. } Else if (action_device_admin_disabled.equals (Action )){
  24. Ondisabled (context, intent );
  25. }
  26. }


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 );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.