What is wake lock in android - devrath/AndroidQuestionsAlchemy GitHub Wiki
What is wakelock
- It is a mechanism that allows the device to stay awake and prevents the device from entering the sleep mode
- In normal circumstances, Android devices aim to conserve power by entering sleep mode when the screen is off and the device is not actively performing tasks.
- Certain apps and services require the device to be active, Even if the screen is off. In Such scenarios we use Wekelock.
Examples of usages
- Music players
- Alarms
- Other tasks
Be cautious on power consumption
- It's important to use wake locks judiciously, as holding wake locks unnecessarily can lead to increased power consumption and reduced battery life. Developers should carefully manage wake locks to balance the need for the device to stay awake with the goal of power efficiency
Example where I have tried
Usage in Exo-Player
Code uasge
// Get the PowerManager
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
// Acquire a wake lock
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "YourApp:WakeLockTag");
// Initiate it
wakeLock.acquire();
Declare in manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />