SoFunction
Updated on 2025-04-04

3 ways to ban screen hibernation by Android apps

When developing Android applications, sometimes it is necessary to prohibit hibernation when running in the foreground of the application. The following methods are for reference.

Method 1:Hold WakeLock

Add a sleep lock, which must appear in pairs.

private WakeLock mWakeLock = null;
 
private void acquireWakeLock() {
  if(mWakeLock == null) {
  
    PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
    
    mWakeLock = (
      PowerManager.PARTIAL_WAKE_LOCK | 
      PowerManager.ACQUIRE_CAUSES_WAKEUP, 
       ().getCanonicalName());
       
    ();
  }
 
}
 @Override
 protected void onResume() {
   ();
   acquireWakeLock();
 }

 @Override
 protected void onPause() {
   ();
   releaseWakeLock();
 }

private void acquireWakeLock(){
 if(mWakeLock != null) {
 ();
 }
}
private void releaseWakeLock() {
  if(mWakeLock != null) {
    ();
    ///mWakeLock = null;
  }
}

existonResumeas well asonPausePerform the corresponding operation

Add permissions to the file:

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

The relevant flags when getting WakeLock are as follows:

  • PARTIAL_WAKE_LOCK: Keep the CPU running, the screen and keyboard lights may be turned off.
  • SCREEN_DIM_WAKE_LOCK: Keep the CPU running, allowing the screen to be displayed but may be grayed out, allowing the keyboard light to be turned off
  • SCREEN_BRIGHT_WAKE_LOCK: Keep the CPU running, allow the screen to be highlighted, allow the keyboard light to be turned off
  • FULL_WAKE_LOCK: Keep the CPU running, keep the screen highlighted, and the keyboard lights also keep bright.

PS: Now the official does not recommend using this method to keep the screen bright, it is recommended to change it to the following two methods

Method 2:Set flag on Window

Set Windows properties in Activity layout for control, but must be executed before loading the layout. The limitation of this method is that it is only valid in the Activity class and is invalid when the Activity exits.

getWindow().setFlags(.FLAG_KEEP_SCREEN_ON, 
     .FLAG_KEEP_SCREEN_ON); 
setContentView(.***);  

//or the settings below are the same//getWindow().addFlags(.FLAG_KEEP_SCREEN_ON);

This method does not require application permission, and it is also an official recommended approach

The advantage of this method is that unlike wakelocks, it does not require specific permissions, and in the operation of changing different applications, the system will manage it without worrying about unused resources that are not released.

You don't need to clearFLAG_KEEP_SCREEN_ONLogo unless you no longer need to stay on the screen while you run the application. When the application enters the background or returns to the foreground, the window manager is responsible for ensuring normal event handling, but if you explicitly want to clear this Peugeot and allow the screen to go out, you can useclearFlags() method

as follows:

getWindow().clearFlags(.FLAG_KEEP_SCREEN_ON)

You can control the screen to turn off

Method 3:Add properties at the top level in the interface layout xml

Add properties to the View layout. The advantage of this method is that the screen does not necessarily need to be always on when the Activity interface is running to be effective. You can set it independently after the View is started.

Added to the xml layout of View:

In XML file, use the android:keepScreenOn property

<relativelayout xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
 ...
</relativelayout>

This settingandroid:keepScreenOn=“true” Equivalent toFLAG_KEEP_SCREEN_ON

Whether the execution in the code keeps the screen on:

(true);
(false);

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.