We all want good power efficiency right..? Like batteries that last a long time. We want to build really cool apps and we do not want the user to manage between these things. Like do I want to build cool apps, or do I want power efficiency. It’s terrifically difficult to make that choice. So, as a developer, you should have to closely manage how your apps behave on a user’s device running Android P version.
Android engineers working very hard to optimize the battery performance. This year in I/O 2018 Google introduced Adaptive Battery feature in Android P.
Adaptive Battery In Android P:
Google engineers co-developed this feature in Android P called Adaptive Battery. It intelligently aligns power consumption with app usage. Apps can still run in the background when they need to, and the user doesn’t need to micromanage the app. So, the adaptive battery uses the concepts of App Standby Buckets.
App Standby Buckets:
App standby buckets are for the app which runs in the background. There is four type of buckets which, I’m going to explain below. Each bucket has different limits on background activity. Jobs, Alarms, Firebase Cloud Messaging, and Network are all restricted in Android P, depending on which bucket the app is in. Every app on the device is assigned to one of these buckets.
- Active Bucket: Apps in the active buckets have no background restrictions.
- Working Set Bucket: The Working set has some restrictions on Jobs and Alarms.
- Frequent Bucket: The Frequent has restrictions on Jobs, Alarms and Firebase Messaging with high priority. In the frequent bucket, messages will be treated as the normal priority, so that they batch together with other messages to save battery.
- Rare Bucket: Apps in the Rare buckets will have the most restrictions. The rare apps cannot use the Alarms, Jobs, Firebase Messaging and Network also.
Note: One thing to remember is that once this device is plugged into power, all of the restrictions are lifted.
Apps Standby Buckets Example:
Let’s say currently, I’m using the Instagram app. The Instagram app is going to be in the Active Bucket. But in a few days time Adaptive Battery is going to automatically determine that I’m likely not to be using this app and put it into the Rare Bucket. So, it is not necessary for consuming resource like a battery.
Standby Buckets adb Commands:
There are adb commands that you can use to test your app in each bucket to make sure that it performs as expected. If you’re not sure how to work with adb or how to enable adb to go and check out this link. The following shows the adb commands.
adb shell dumpsys battery unplug adb shell am get-standby-bucket <package-name> 10 ACTIVE // bucket value 20 WORKING_SET 30 FREQUENT 40 RARE // set bucket value adb shell am set-standby-bucket <package-name> <bucket_value> adb shell dumpsys battery reset
Machine Learning Model:
The Adaptive Battery use the machine learning model to determine which app will go into which bucket based on their usage. The model is built by Android and DeepMind to predict which app goes into which bucket. The model itself capture the nuance of how the user behaves with their apps. It is used to predict their probability that an app will be opened in a given interval. Now, all of this is happening on the device using Tensorflow. Google engineers said in an I/O presentation.
We are doing this machine learning on a single device. So, we have to be very careful that we’re not spending more power than we’re saving.
Example Of Machine Learning Model:
If I use an app at 8’o clock every morning, then I probably quite likely to open it tomorrow 8’o clock as well. But if you only use a certain app on weekend like a game or a travel app, then you’re probably not going to open it on Monday morning.
Now the model is able to capture this nuance. The model looks at the user’s behavior and outputs a probability of when the user next going to open a particular app.
Fairness & Privacy:
There is no favoritism of one app over another. If you use particular app exactly the same way that you use a different app, then the model will output the same predictions and they will be assigned to the same buckets. It’s privacy-sensitive there’s no personal identification information used by the model. All information is removed before the model is trained.
Background Restrictions For App:
If a user decides to restrict your app from settings then there are few things to know for a developer.
- Background Jobs, Alarms, Services & Network access is not available to those in the backgrounds.
- The location will not be delivered.
- If an application is in the background and restricted, it won’t be able to use foreground services.
There are adb commands that you can test to put your app into a restricted state.
// To apply background restrictions. adb shell appops set <package-name> // To remove background restrictions. add shell appops set <package-name>
- If you wanna think about doing anything in the background, then WorkManager is the approach.
- If you think something that is really important and must happen now, of course, you can have the ability to use foreground service. But if you use foreground service you have to tell the user, why you are using it. And you need to justify yourself why you think it is important.
- So, if none of the above works, think about actually doing the work when the app is launched and being active by the user.
If you like what you read gives it a thumbs up and keep reading.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, we will receive an affiliate commission. Regardless, we only recommend products or services we use personally and believe will add value to our readers.