WakeLocks

Some applications, in some cases, need to prevent the device from going to sleep to maintain a good user experience even if the user is not interacting with the device for an extended period of time. The simplest example, and probably the most relevant, is when a user is watching a video or movie on the device. In such a case, the CPU needs to decode the video while the screen needs to be on for the user to be able to watch it. Also, the screen should not be dimmed while the video is playing.

The WakeLock class allows for such scenarios, as shown in Listing 7–19.

Listing 7–19. Creating WakeLock

    private void runInWakeLock(Runnable runnable, int flags) {         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); ...

Get Pro Android Apps Performance Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.