Android system comes with lock WalkLock and KeyguardLock usage examples detailed

  • 2021-01-18 06:38:39
  • OfStack

This article illustrates the use of WalkLock and KeyguardLock locks in Android. To share with you for your reference, as follows:

WalkLock - As the name implies, the wake lock is used to light up the screen
KeyguardLock - As the name implies, keyboard lock is used to unlock the keyboard

Detailed introduction:

1: WalkLock wake up lock

- Does the WalkLock actually light up the screen?

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

PowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Gank");

PowerManager. FULL_WAKE_LOCK This parameter is the degree of light of the phone (what Cpu, screen brightness, keyboard light).
PowerManager.ACQUIRE_CAUSES_WAKEUP The key is to understand this parameter.
WalkLock lighting up the screen doesn't really light up the screen, you can think of it as lighting up the screen via the Android component (Activity).

If a notification wants to light up the screen, the question is, will it light up? Definitely not.

But with the PowerManager.ACQUIRE_CAUSES_WAKEU parameter, you can light up the screen. It enables the WalkLock to light up the screen without having to rely on components.

- How does WalkLock get screen status?

PowerManager. isScreenOn () method; This method returns true: the screen is awake and returns false: the screen is dormant

- WalkLock wake-up and sleep method?

WalkLock.aquire () wakes up the screen from the screen sleep state
WalkLock.release () Sleeps the screen with the screen on.

There is one caveat to the WalkLock.release () method:

For example, WalkLockA objects wake up the screen and then put it to sleep. ok is fine

The screen itself is awakened. WalkLockA objects have not awakened the screen. If WalkLockA objects try to make the screen sleep. An exception will be raised UnLock Sreen.

2: KeyguardLock keyboard lock

- Is the current screen unlocked for KeyguardLock?

KeygroundManager.inKeyguardRestrictedInputMode () Returns true to indicate that the keyboard is locked, and false to indicate that the keyboard is unlocked

- KeyguardLock unlocks and locks the screen?

KeyguardLock.disableKeyguard () Unlocks the keyboard
Lock the keyboard KeyguardLock. reenableKeyguard ()

KeyguardLock does not have the above wake-up lock problem, that is, no matter whether your keyboard is unlocked by KeyguardLockA or not, you can call the reenableKeyguard () method on the KeyguardLockA object without an exception.

These two locks are a little more conceptual. 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, 1 for loop 1 straight lock your screen, you can cry no tears)

You can control your own lock and don't think about anyone else's.

Finally, summarize the usage:

These two locks work together, so you don't want the screen to be dark when you unlock it. You want the screen to sleep when you turn off the keyboard lock.

Question:

1: I tried to turn off the screen manually, but it kept on for a minute.
2: If the phone shuts off the screen automatically, it won't have this problem.


public void unlockScreen() {
  //  To obtain PowerManager An instance of the 
  PowerManager pm = (PowerManager) mContext
      .getSystemService(Context.POWER_SERVICE);
  //  get 1 a WakeLock Wake up the lock 
  mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
      | PowerManager.ACQUIRE_CAUSES_WAKEUP
      | PowerManager.ON_AFTER_RELEASE, "SimpleTimer");
  if (!mWakelock.isHeld()) {
    //  Wake up the screen 
    mWakelock.acquire();
  }
  //  To obtain 1 a KeyguardManager An instance of the 
  km = (KeyguardManager) mContext
      .getSystemService(Context.KEYGUARD_SERVICE);
  //  get 1 A keyboard lock KeyguardLock
  mKeyguardLock = km.newKeyguardLock("SimpleTimer");
  if (km.inKeyguardRestrictedInputMode()) {
    //  Unlock the keyboard 
    mKeyguardLock.disableKeyguard();
  }
}

mWakelock.isHeld()) is used to determine whether the screen is currently in hibernation. Android 2.1 API Level7 has been added to determine whether the screen is in hibernation by using public boolean isScreenOn ().

The code for the lock screen is


public void lockScreen() {
  // release screen
  if (!km.inKeyguardRestrictedInputMode()) {
    //  Lock the keyboard 
    mKeyguardLock.reenableKeyguard();
  }
  //  Sleep Screen 
  if (mWakelock.isHeld()) {
    mWakelock.release();
  }
}

I hope this article is helpful to Android program design.


Related articles: