The battery life of an Android phone is dependent on many factors, a few of them are, screen brightness, fast processors, apps running in the background, thin bodies, and many more. Being an app developer, all hardware constraints are out of one’s hands and cannot be changed. What an app developer can do is, make changes in the app such that it requires minimum battery usage and doesn’t drain the battery. There are many methods to do so and a few are explained below.
There are several operations in an app that are very intensive on the battery, they require a large amount of battery and can cause drain issues. In the lazy first method, such operations are reduced and optimized. The main steps in a lazy first method are
- Defer: Any battery-intensive operation can be delayed if it’s not of utmost urgency. It can be executed when the device is plugged into power.
- Reduce: Any redundant operation can be removed, it saves the battery by escaping recomputation. Data that is required often can be put into cache, instead of downloading again and again.
- Coalesce: Many battery-intensive operations can be grouped into one operation. Separate execution requires waking the device again and again. Instead of this, similar types of operations can be clubbed and executed at once.
These steps should be used when the app requires CPU time, screen, internet use, etc. The lazy first method can be applied in the app to make it battery efficient.
Power saving methods
Many Power saving methods are introduced by Android in recent times. After Android 6.0 android came up with two big battery saver methods, Doze and App Standby.
- Doze defers the background network activities and CPU activities of the entire device when it is unused for a long time. This helps in saving a lot of battery life. It’s similar to a device going into sleep mode.
- App standby work on apps individually. The app which has not been used for a long time can save a lot of battery. This defers the background activities of such apps.
When the user doesn’t make a touch in the app for a long time, the system determines that the app is idle now. It also allows the system to determine an app’s idleness when –
- The user does not launch the app.
- The app does not generate a notification.
- The app doesn’t have a process running in the foreground.
- It is not a device admin app. These apps always run in the background and can never be terminated or enter into the app standby state. The New Android version allows the user to keep running an app in the background for always.
When the device is plugged into power, all the stand-by apps are released. They are allowed to access network connections and to complete the job that was left before it went into standby mode. There are some exemptions from the stand-by category other than the device admin apps. These are
- Instant messaging or calling app – They use Firebase Cloud Messaging ( FCM) high-priority messages to wake up the app and access the network.
- Task automation app – The core function of these apps is to schedule the actions that are automated such as instant messaging and calls.
- Peripheral device companion app – The core function of these types of apps is to maintain a continuous connection with the peripheral device in order to provide them with internet access. Example – Apple watch, fitness bands.
How to test the app with app standby
- Get a device having Android 6.0 or later.
- Connect the device to your system.
- Install the app and leave it active.
- Run this command to force your app on standby
$ adb shell dumpsys battery unplug
$ adb shell am set-inactive <packageName> true
- Wake up the app with this command
$ adb shell am set-inactive <packageName> false
$ adb shell am get-inactive <packageName>
- Confirm that the app comes out of standby mode with all functions working correctly