Android O is the code name of the new Android release that is currently under development. Android O is now available as Developer Preview, meaning that when you are interested you can already download this version of Android. The final release has been scheduled for the third quarter of this year. Since it is a preview version, it isn’t good enough for public use. However, it does allow Google to obtain feedback from the actual users.

Android always allowed background processing in the app. This differs from for example iOS, where it is more restricted. Background processing can be used for example to keep the contents of the app synchronized, finish a pending upload or to track the actions of the user. Having the possibility to do operations in the background can lead to issues related to memory usage, causing lower device performance, and higher battery usage. To let Android battery times come closer to that of an iPhone a couple of changes have been made to the operating system over time. These changes make it easier for developers to pick better times to do operations in the background, but often new restrictions are added as well.

Apps can continue to do a lot in the background, also in the new Android O release. However for it to work, it is required that you use the right APIs that Android specifically provides for this. There are two APIs we as Plot Projects are most interested in, namely Geofencing and background jobs for updating the app database in the background. Another API that sparks our interest is the Firebase Messaging API, that allows waking an app remotely via the internet. This API can be used to either push a notification, but also to push data to the devices.

Historical overview

What worked in the past

In the early versions of Android, not a lot of restrictions were in place. Every app was basically allowed to do whatever it wanted to do in the background. Still, when Android would run out of resources it could stop processes to make room for processes with a higher priority.

Apps could poll in the background for changes, keep updating the location, keep an internet connection open. This both uses a lot of memory, as it will be hard for Android to pause processes that are continuously active in the background, and uses a lot of battery, as the device couldn’t be put to sleep. Not an ideal situation.

What works now

In Android Lollipop and also via Google Play Services more APIs have come available to perform operations in the background. However, there aren’t a lot of restrictions put in place to ensure apps only work during these efficient time windows.

There is an API to inform apps when the user has entered a specific location. This API is called the Geofencing API and is provided by the Google Play Services. A single service running on the device polls whether the device enters one of the regions monitored that are registered by one of the apps running on the device. Because it can poll for all apps combined, not all apps have to be woken up regularly to poll themselves and it is, therefore, more memory and battery efficient. An improved version of this API is called the Awareness API, where you can place more restrictions on when it can trigger. For example, you can say that it should only trigger for devices in a certain area, during the day and when the weather is sunny.

Another improvement is the introduction of the Job Scheduler. This is a mechanism for an app to schedule background updates. The app specifies some constraints, for example, run it once a day when the device has an internet connection, and then Android picks the right moment to perform this job. Because of Android scheduling this job, it can check first whether the device is idle so it has the resources to perform the job and it can combine the jobs of multiple apps into a single execution time window. This way users notice less of the background processes and it consumes less battery. Also for developers it is easier, as you have to think less about when to schedule your work.

Recently there has been another change that leads to a battery improvement: Background timers have become way less precise. Work of multiple apps is combined into a single time window. A big restriction in background execution that has been introduced in Marshmallow is Doze mode on top of the timer restriction. This limits background activity when the device is idle for a longer period of time and not plugged into power. This way the battery consumption of a device that is not in use is significantly lower.

What works when O is released

In previous Android versions improvements were made to the Android API to make it easier to do work in the background efficiently. These improvements were added alongside the old inefficient APIs, which kept working. Android O will start disabling these APIs to ensure more device owners can benefit from these improvements. This will result in a phone battery that lasts longer and foreground apps that have no interference from apps running in the background.

In Android O a number of triggers that a device could use to execute in the background will be removed. Unbound services (services started from an Intent) can no longer run in the background and creating Intent Filters inside your manifest file will no longer work for most implicit broadcasts. More is explained in the documentation on the page Background Execution Limits. Instead, apps should switch to APIs like the Geofencing API (the new Awareness API is also an option) or the Job Scheduler API. This way a lot fewer apps have to be kept in memory or be brought into memory to respond to an Intent broadcast and therefore your device will be more responsive.

The limit will only apply to apps that target API Level 25, which is the new API level of Android O. Therefore older apps will continue to work while in the background. If your app has to target API Level 25, but also has to work in the background that doesn’t fit for example the Geofencing API or the Job Scheduler API, you could consider placing a “sticky” notification. With the method startServiceInForeground you can start a service while in the background, but Android considers it a foreground process because a notification is visible. It makes it harder for Android to free up memory when a foreground process needs it, affecting system-wide performance, so only do it when really needed.

I want to do things in the background, what do I do?

To ensure it works in the current and future versions of Android, look for trigger based APIs. Examples of these include the Geofencing API, the Awareness API or the Job Scheduler. Using the AlarmManager is also still possible, but don’t be surprised that your app won’t get woken up often.

There are plenty of guides and talks online explaining how to do this. For Geofencing there is a training guide online. For the Awareness API you can look at the guide or at the talk they gave at last year’s Google I/O. For the Job Scheduler the guides available are a bit limited. You can find a comparison between the Job Scheduler and other scheduling mechanisms on Android here. You can use that page as a start to read more about how to use it. To push new content to your app straight when it is available Firebase Messaging can be used. No need to keep polling your backend for new content. This is also easy to set up by following their guide.

If you don’t want to think about all this stuff, you could also consider using an existing tool that takes care of all this. In the end, it is a lot of work to get it working in the right way and an efficient way. In case you need geofencing, you should definitely consider our library: just create an account and start using geofencing straight away. No need to set up synchronization with your backend yourself and no need to fine tune the usage of the Geofencing API. Also, you get along analytics for free.