Using Android Wakelock – Staying up all night long

One of the think you as an Android developer should properly not do is prevent the Android system from shutting down certain components to save energy.

But sometimes you need to prevent the system from sleeping, e.g. during an important update or a critical download. If you choose to do so here is how you can aquire a WakeLock:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
		pm.SCREEN_DIM_WAKE_LOCK, "My wakelook");
// This will make the screen and power stay on
// This will release the wakelook after 1000 ms

// Alternative you can request and / or  release the wakelook via:
// wakeLock.acquire(); wakeLock.release();

Again, you most likely do not want to do this. Acquiring a wakelock interferes with the power management of the system and might surprise your users.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, He is the primary author of With more than one million visitors per month this website is one of the central sources for Java, Eclipse and Android programming information.
This entry was posted in Android and tagged . Bookmark the permalink.