Luis Cruz
Luis Cruz

Reputation: 403

Does Android have a different behavior for WakeLock when the device is connected to power source?

To give some context: I am trying to measure the energy consumption of best practices in Android apps. I have refactored an app that was using a WakeLock to keep the screen on and I use the flag FLAG_KEEP_SCREEN_ON instead. Surprisingly, I am only able to have a really small improvement on energy consumption.

I was expecting the CPU to be able to go into a low power state, which would spend less energy.

Is there a different behavior for CPU power management in these conditions, or the use of this FLAG is not expected to have a reasonable effect?

I am measuring with the device ODROID-XU which has to be connected to power. The Android version is 4.2.2 - API level 17.

Upvotes: 0

Views: 312

Answers (2)

Luis Cruz
Luis Cruz

Reputation: 403

After doing some research on this and based on previous comments in this question, I have found an explanation that is clear to me.

The following article explains how a WakeLock should be used in order to make sure it is properly released when it is no longer necessary: http://vliux.me/android/android-power-consumption-and-wakelock/

The author explains that the usage of a Wakelock is not an easy task and is prone to errors that can drain the battery. When we use the flag FLAG_KEEP_SCREEN_ON the system (WindowManager) handles the locks for us. Thus, we make sure that the Wakelock will be properly released.

In addition, as mentioned by @Dodge, using the flag FLAG_KEEP_SCREEN_ON does not require a special permission.

This is also supported in the following answer: https://stackoverflow.com/a/4376967/1228682 .

Note: There is a lint check for incorrect wake lock usage that suggests the use of FLAG_KEEP_SCREEN_ON: Find "Wakelock" at lint docs: http://tools.android.com/tips/lint-checks.

Upvotes: 0

Atiq
Atiq

Reputation: 14825

The reason you didn't find much difference is

WakeLock prevents the device from going to sleep when the device's screen is off, while when your using FLAG_KEEP_SCREEN_ON the display is also using the power along with the app, and believe me it takes reasonable amount of power.

So the result would be almost same, while using FLAG_KEEP_SCREEN_ON might result in bad UX.

Upvotes: 0

Related Questions