Search code examples
androidandroid-wake-lockenergyodroidgreensoftware

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.


Solution

  • 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.