The system comes with the lock in Android: Walklock, Keyguardlock

Source: Internet
Author: User

The Android system comes with two locks:

(1) Walklock: Wake-up lock, light screen to use;

(2) Keyguardlock: keyboard lock, unlock the keyboard with;

Walklock Wake-up lock

(1) Walklock really can light the screen?

The answer is yes, but sometimes why not light the screen, this is the parameter setting problem.

Powermanager.newwakelock (Powermanager.full_wake_lock | Powermanager.acquire_causes_wakeup, "Gank");

The key to Powermanager.acquire_causes_wakeup is the understanding of this parameter.

  

Walklock The screen is not really going to light up the screen, you can understand that it goes through the Android component (Activity) to light up the screen.

If there is a notification (notication) want to light up the screen, then the problem is, it can light it? Definitely not.

But with this powermanager.acquire_causes_wakeu parameter, you can light up the screen. It allows the walklock to light the screen without relying on components anymore.

Related parameter Description :

partial_wake_lock: Keep the CPU running, and the screen and keyboard lights may be off.

screen_dim_wake_lock: Keep the CPU running, allow the screen to be displayed but may be gray, allow to turn off the keyboard light

screen_bright_wake_lock: Keeps the CPU running, allowing the screen to be highlighted, allowing the keyboard light to be turned off

full_wake_lock: Keep the CPU running, keep the screen highlighted, and the keyboard lights remain bright

acquire_causes_wakeup: Normal wake locks don ' t actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag would force the screens and/or keyboard to turn on immediately and when the WakeLock is acquired. A Typical use would is for notifications which is important for the user to see immediately.

on_after_release: This flag is set, the user activity timer'll be reset when the WakeLock is released, causing T He illumination to remain on a bit longer. This can is used to reduce flicker if you is cycling between wake lock conditions.

(2) Walklock How to get the status of the screen?

Powermanager.isscreenon () method; This method returns true: The screen is wake-up return false: the screen is dormant

(3) How is Walklock awakened and dormant?

Walklock.aquire () wakes up the screen in the state of the screen hibernation

Walklock.release () causes the screen to hibernate while the screen is lit.

Walklock.release () This method has a need to be aware of:

For example: The Walklocka object wakes the screen before it sleeps, OK.

The screen itself is the wake state, the Walklocka object does not wake the screen, and the Walklocka object attempts to hibernate the screen. will produce an exception UnLock sreen.

Keyguardlock Keyboard Lock

(1) Keyguardlock get the current screen unlocked?

Keygroundmanager.inkeyguardrestrictedinputmode () Returns true to indicate that the keyboard is locked and returns false for the keyboard Ching

(2) Keyguardlock to unlock and lock the screen?

Keyguardlock.disablekeyguard () Unlocking the keyboard

Keyguardlock.reenablekeyguard () lock keyboard

Keyguardlock does not have the above wake-up lock problem, that is, whether your keyboard is unlocked by Keyguardlocka, you call the Keyguardlocka object's Reenablekeyguard () method will not have an exception.

These two locks have some conceptual understanding, and if you think you've got a keyboard lock object, you can lock the screen. This is a big mistake.

You can't lock the screen that other programs open (if you can, a for loop keeps your screen locked and you cry without Tears)

You can control your own locks, don't think about other people's locks.

Summary:
Generally these two locks are used together, you must unlock the screen when you do not want the screen dark piece. When you close the keyboard lock, you want the screen to hibernate at the same time.

Questions:
1: I try to turn off the screen manually, but always on for a little while.

2: If the phone automatically turns off the screen, there will be no problem.

 Public voidUnlockscreen () {//get an instance of PowerManagerPowerManager pm =(PowerManager) mcontext. Getsystemservice (Context.power_service); //get a wakelock wake-up lockMwakelock =Pm.newwakelock (Powermanager.full_wake_lock|Powermanager.acquire_causes_wakeup| Powermanager.on_after_release, "Simpletimer"); if(!Mwakelock.isheld ()) {            //Wake-up screenMwakelock.acquire (); }        //get an example of a KeyguardmanagerKM =(Keyguardmanager) mcontext. Getsystemservice (Context.keyguard_service); //get a keyboard lock KeyguardlockMkeyguardlock = Km.newkeyguardlock ("Simpletimer"); if(Km.inkeyguardrestrictedinputmode ()) {//Unlocking the keyboardMkeyguardlock.disablekeyguard (); }    }


Note: the Mwakelock.isheld () used here to determine whether the screen is currently dormant, from the Android 2.1 API Level7 to add a judging screen is lit state can use the public boolean Isscreenon () This method.
The code for the lock screen is

     Public void lockscreen () {        //  Releasescreen        if (!  Km.inkeyguardrestrictedinputmode ()) {            //  lock keyboard             Mkeyguardlock.reenablekeyguard ();        }         // make the screen hibernate        if (Mwakelock.isheld ()) {            mwakelock.release ();        }    }

You need to declare in Androidmanifest.xml that the app has permission to set up power management.

<uses-permission android:name= "Android.permission.WAKE_LOCK"/>
<uses-permission android:name= "Android.permission.DEVICE_POWER"/>

Tip: When you add the last permission, you may be prompted with "Permission is just granted to system apps" and you need clean

Reprint Address: http://mysuperbaby.iteye.com/blog/1460948

(turn) the system's own lock in Android: Walklock, Keyguardlock

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.