Android programmed to disable the system lock screen and unlock the bright screen

  • 2020-11-25 07:34:18
  • OfStack

This article describes the Android programming method to disable the system lock screen and unlock the bright screen. To share for your reference, the details are as follows:

Requirements:

At some point the task finishes, the screen is closed, and at some point the screen is opened again to continue the task

Usually, the system will lock the screen automatically if the screen is not always on. After locking the screen, it cannot unlock the screen from the code (question), so my practice is to only let the system close the screen, but not lock the screen!


WakeLock lock, unLock;
KeyguardManager km;
KeyguardLock kl;


unLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
//  Gets the keyboard lock manager object 
km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
kl = km.newKeyguardLock("unLock");


void lockScreen() {
  //  Lock screen 
  kl.reenableKeyguard();
  //  The release of wakeLock Turn off the lights, 
  if(unLock.isHeld())
    unLock.release();
}
void unLockScreen() {
  //  Light up the screen 
  if(!unLock.isHeld())
    unLock.acquire();
  //  unlock 
  kl.disableKeyguard();
}

Test code. After invoking the test code, you manually press the power key to lock the screen. After 5s, you will see the screen light up


{
  lockScreen();
  getListView().getHandler().postDelayed(new Runnable() {
    @Override
    public void run() {
      unLockScreen();
    }
  }, 5000);
  break;
}

I hope this article has been helpful in Android programming.


Related articles: