Techniques to Reduce Android Device Power Consumption and Prevent Shutdown During Automated Testing
This guide explains how to lower Android device power usage and avoid unexpected shutdowns in batch testing by managing battery thresholds, adjusting screen brightness, disabling background apps, setting screen timeout, enabling automatic time, and controlling audio settings through shell commands and reflection.
During large‑scale Android testing, devices often run out of battery and shut down, increasing operational costs; this article describes methods to reduce power consumption and prevent power‑off interruptions.
By defining low (20%) and normal (60%) battery thresholds, tasks are paused when devices fall below the low level and resumed when above normal; the current battery level is obtained via adb shell dumpsys battery .
~$ adb shell dumpsys battery
Current Battery Service state:
AC powered: false
USB powered: true
Wireless powered: false
Max charging current: 0
Max charging voltage: 0
Charge counter: 37000
status: 5
health: 2
present: true
level: 100
scale: 100
voltage: 4358
temperature: 300
technology: Li-polyTo lower screen power draw, set the brightness adjustment mode to manual and then modify the brightness value (0‑255) using shell commands.
# Set brightness adjustment mode to manual (0) or automatic (1)
settings put system screen_brightness_mode 0
# Get current brightness mode
settings get system screen_brightness_mode # Get current brightness (0‑255)
settings get system screen_brightness
# Set brightness to 150 (0‑255)
settings put system screen_brightness 150After tasks finish, close all third‑party apps and lock the screen to avoid background power drain. List installed third‑party packages with pm list packages -3 and terminate each using am force-stop .
$ adb shell pm list packages -3
package:com.sankuai.meituan
package:com.tencent.mm
package:com.liuzh.deviceinfo
package:com.ct.client adb shell am force-stop com.ct.clientSet the device screen‑off timeout to 30 seconds to keep the device awake during script execution.
adb shell settings put system screen_off_timeout 30Enable automatic time synchronization to avoid time‑related errors.
adb shell settings put global auto_time 1Adjust the device volume via reflection: obtain the IAudioService object, query the current ringer mode, and set it to normal, then set specific stream volumes.
Method getServiceMethod=Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String.class);
IBinder binder = (IBinder) getServiceMethod.invoke(null, "audio");
Method asInterfaceMethod = Class.forName("android.media.IAudioService$Stub").getMethod("asInterface", IBinder.class);
IAudioService iAudioService = (IAudioService) (IInterface) asInterfaceMethod.invoke(null, binder); int mode = iAudioService.getRingerModeExternal(); iAudioService.setRingerModeExternal(AudioManager.RINGER_MODE_NORMAL, "com.android.settings");
iAudioService.setRingerModeInternal(AudioManager.RINGER_MODE_NORMAL, "com.android.settings");Note: on some Huawei devices this may raise a SecurityException due to missing STATUS_BAR_SERVICE permission.
# Huawei specific error example
java.lang.SecurityException: request policy access status for another package: uid 2000 does not have android.permission.STATUS_BAR_SERVICE.
at android.os.Parcel.createException(Parcel.java:1953)
...Finally, set stream volume using iAudioService.setStreamVolume(streamType, volume, 0, null) , where streamType can be AudioManager.STREAM_MUSIC , STREAM_ALARM , etc.
// Example: set music volume to level 5
iAudioService.setStreamVolume(AudioManager.STREAM_MUSIC, 5, 0, null);360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.