Three ways to keep the screen always on in Android

Table of contents

1. Set Flag

2. Use the attribute methods in the View class

1. Set in Xml layout:

2. Set in function code:

3. Wakelock locking mechanism

1 Introduction

2. Function

3. Classification mark

4. How to use

(1) Set permissions in AndroidManifest.xml

(2) Obtain the instance object of the WakeLock class through the PowerManager class

(3) Precautions

(4) Optimization measures

(5) Summary


1. Set Flag

This method is officially recommended, it is the simplest and does not require permission application.

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

2. Use the attribute methods in the View class

1. Set in Xml layout:

In the xml file referenced by the main interface, find the top-level layout and add the following attributes:

android:keepScreenOn="true"  

2. Set in function code:

topLayout.setKeepScreenOn(true);

3. Wakelock locking mechanism

1 Introduction

In Android application development, Wakelock is widely used to keep the screen or CPU awake so that the application can continue to perform background tasks until the task is completed. Wakelock is a concept created to solve some specific application scenarios. Its function is to keep the screen, CPU or some other machine status running to meet some specific needs.

WakeLock is a set of mechanisms provided by the Android framework layer. Applications can use this mechanism to control the status of Android devices. The device status here mainly refers to the opening and closing of the screen and the keeping of the CPU. Simply understand that WakeLock is a means to keep the system "awake".

2. Function

In some cases, some apps prevent users from going to sleep even if they don't interact with them for an extended period of time. An example is that when we are watching a video, the mobile phone screen should remain on. When the phone's screen remains off for a period of time, the system will enter sleep mode, and some background tasks may not be executed properly, such as network downloads being interrupted , music playing in the background being paused, etc. Android designed WakeLock for this purpose. WakeLock In order to solve this kind of problem, as long as the application applies for WakeLock, the system will not go to sleep before releasing WakeLock. Even when the screen is turned off, the tasks to be performed by the application will still not be interrupted by the system.

  •  Keep the CPU awake so that applications can continue to perform background tasks;
  •  Keep the screen awake so that the application can continuously display information to the user;
  •  Keep the device awake to ensure that certain specific tasks can be performed normally.

Wakelock is commonly used in the following scenarios:

  • Music playback: When a music application is playing, it needs to keep the CPU awake so that the music can play normally, and the screen awake so that the user can view the currently playing track;
  • Background tasks: When an application needs to perform certain tasks in the background, it needs to ensure that the CPU is awake so that the task can be executed normally, and it may need to keep the screen awake so that the user can obtain certain information;
  • Location monitoring: When an application needs to monitor the device's location information, it needs to ensure that the CPU is awake so that it can correctly obtain the device's location information.

3. Classification mark

  • PARTIAL_WAKE_LOCK: Keep the CPU running, the screen and keyboard lights may be turned off.
  • SCREEN_DIM_WAKE_LOCK: Keep the CPU running, allow the screen to remain displayed but may be gray, allow the keyboard light to be turned off
  • SCREEN_BRIGHT_WAKE_LOCK: Keep the CPU running, allow the screen to be highlighted, and allow the keyboard light to be turned off
  • FULL_WAKE_LOCK: Keep the CPU running, keep the screen highlighted, and the keyboard light also maintains brightness
  • ACQUIRE_CAUSES_WAKEUP: Force the screen to light up. For example, after the application receives a notification, the screen lights up.
  • ON_AFTER_RELEASE: After releasing WakeLock, the screen does not turn off immediately and keeps the screen on for a period of time.
  • UNIMPORTANT_FOR_LOGGING: Hidden flag, only used at the system level
  • DOZE_WAKE_LOCK/DRAW_WAKE_LOCK: Hidden classification, only used at the system level
  • PROXIMITY_SCREEN_OFF_WAKE_LOCK: Turn off the screen based on proximity sensor. The most typical usage scenario is that when we make a phone call close to our ear, the screen will automatically turn off.

4. How to use

       WakeLock is an internal class of PowerManager and its code path is at:

  frameworks/base/core/java/android/os/PowerManager.java

(1) Set permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
(2) Obtain the instance object of the WakeLock class through the PowerManager class

         You can usually call the acquire() method in onResume() and the release() method in onPause() method.    

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "tag:CpuKeepRunning");

//或 执行 wakeLock.acquire();
wakeLock.acquire(1000 * 60);

@Override  
protected void onResume() {  
    super.onResume();  

    //可以在onResume()中执行acquire()方法
    if (wakeLock!= null) {  
        wakeLock.acquire();  
    }  
}  
  
@Override  
protected void onPause() {  
    super.onPause();  
    if (wakeLock!= null) {  
        wakeLock.release();  
    }  
}  

(3) Precautions
  • Officials no longer recommend using this method to keep the screen on. Improper use will cause the app to become a "battery killer". Please try the other two methods.
  • There are two ways to apply for WakeLock: acquire() and acquire(long timeout), the latter is relatively safer. If you forget to release WakeLock, the system will automatically release it after the timeout period. Under normal circumstances, WakeLock needs to be released as soon as possible after using it. If you forget to release it, it will quickly run out of power;
  • To prevent problems, it is recommended to use the WakeLock.acquire() version with timeout, which will release the WakeLock after the time limit is exceeded. For example, when playing a video, you can use the duration of the video as the timeout, so that the WakeLock will be automatically released after the video is played. We can see from the source code that its principle is to send a delay message through Handler.postDelay() when calling acquire() to apply for a WakeLock to automatically release it after the time is reached;
  • Please do not use indefinite Wakelock, which will cause the device and applications to not sleep for a long time, and will eventually drain the device's battery;
  • Use SCREEN_ON, SCREEN_DIM, SCREEN_BRIGHT and other types of Wakelock only when necessary. Other types of Wakelock can better meet the needs of the application;
  • Avoid using Wakelock if possible unless your app doesn't work properly or does what it needs to do.
(4) Optimization measures

        To minimize battery drain when you use Wakelock, you can perform the following optimizations:

  • Only apply for Wakelock when needed and release it as soon as possible after the task is completed;
  • Use flags to easily check whether the device is awake without having to apply for and release Wakelock frequently;
  • Optimize the use of Wakelock to avoid invalid wake-up operations to reduce battery consumption;
  • Use scheduled tasks such as AlarmManager or JobScheduler instead of Wakelock to better preserve battery life.

(5) Summary

Android Wakelock is a locking mechanism provided by Android that keeps the device awake so that applications can continue to perform tasks in the background. The use of Wakelock is very simple. You only need to call the Wakelock.acquire() method where you need to keep the awake state. When you don't need to keep the awake state, you can call the Wakelock.release() method to end it. When using Android Wakelock, you need to pay attention to several issues, such as calling the Wakelock.release() method to release Wakelock, avoiding abuse of Wakelock, etc.

Guess you like

Origin blog.csdn.net/IT666DHW/article/details/131853090