The method that the Android app running in the background and hibernating can still run (make sure it runs all the time)

In the player app, because the background service needs to be used to play music, once it enters the dormant state, the process may be killed,
so it is necessary to keep the service running without being killed.

The related API of WakeLock in android can ensure that the background tasks in the application are running all the time, so that the application has the ability to control the sleep state of the APP.

WakeLock lock mechanism

When the app applies for the WakeLock permission to call the relevant API, WakeLock will prevent the app from hanging, and the system cannot go to sleep. Even when the screen is off, the tasks to be performed by the app will not be interrupted. When all WakeLocks are released (unlocked/timed out), the system will suspend and start the hibernation mechanism to enter hibernation.

Preferred Application Permissions:

<!--WakeLock需要的权限-->
<uses-permission android:name="android.permission.WAKE_LOCK"/>

WakeLock classification and Flag

1. Classification

PARTIAL_WAKE_LOCK: 灭屏,关闭键盘背光的情况下,CPU依然保持运行。
PROXIMITY_SCREEN_OFF_WAKE_LOCK: 基于距离感应器熄灭屏幕。最典型的运用场景是我们贴近耳朵打电话时

Guess you like

Origin blog.csdn.net/baidu_41666295/article/details/122445828