Category: Blog, Android, Development

New Android O – what does it mean to you as a developer?

A few days ago, Google announced info about the new developer preview version of Android O: Android Developers: O-MG, the Developer Preview of Android O is here!

Since Android N, Google is trying to take control over the battery-consuming background processes. It’s caused, for example, by implicit intents and apps registered for receiving broadcasts based on system events. Every event runs many background processes and it can dramatically drain our battery, so it’s natural that Google is trying to improve this part.

In Android O, Google has made some changes to improve the user’s battery and security. This has resulted in limits for any app’s background work. Let’s look at these changes:

Broadcast limitations

Our apps, if targeting Android O, can no longer register implicit broadcasts in the manifest. Of course, you can register them in the manifest, but it will have no effect. You can still register them at the runtime with  Context.registerReceiver(), but it means our receivers’ lifecycle will be bound with Context (so receivers won’t live in the background).
It implies, as a fact, that all of the actions, e.g. CONNECTIVITY_ACTION for checking network state won’t work if run from the manifest, so we need to change this in our apps. Google developers offer us some replacement for this in the form of JobScheduler.

Explicit broadcasts will work when registered in the manifest, as it was before.

Note

Some of the broadcast receivers’ actions are exempted from this rule, for now. You can find them here: Android Developers: Background Broadcasts

No more background services?

Another big change involves how background services will work. If the app is in the background, background services will run on a minimum. When the app goes to the background, it’s still allowed to create and run services for several minutes. After this time, the system stops all of the backgrounds services. It means for us, that the only way for waking up the app from the background will be with the push notifications from FCM or, as previously mentioned, JobScheduler.

Background services can’t be started when the app is in the background, so there is a new method for creating service in the foreground, even if we’re in the background – NotificationManager.startServiceInForeground(). It starts service directly into the foreground state.
You can read more about all of the background limitations here: Background Execution Limits

Location in background reduced

These changes in background services also cause changes in getting the location in the background – now, updates will be made a few times per hour, but as the developers say, this can be still changed during the preview version tests. Important info – this change will be made in the Android O regardless of the app’s target SDK. Foreground app behavior will stay as it is.

Settings and permissions

When it comes to behaviour, there’s a chance of getting a unique device id with Settings.Secure.DEVICE_ID. Previously, the device id was unique per device but now it’s different with every version of the app. It may have some effects on logic in your application, so be aware of it.

Another change in security is in getting the user’s account. Now it’s not enough now to have GET_ACCOUNT permission. The app should now also use AccountManager().newChooseAccountIntent() to be granted access to accounts.

New Notification Channels

Notification Channels allows the user to organize and customize their notifications. You can create multiple channels for different notification types – for news, shares, messages etc. The user will see them grouped by the channels.

notifications inline controls1 crop

The important thing for you is, if you have some notifications in your app, they must have some Notification Channel as they otherwise won’t be shown. So, it’s important for you to set some channel to them.

Checklist before targeting to API 26:

Here’s the list of the main things you should remember before targeting your app to the newest Android O version.

  • Remove broadcast receivers registered for implicit broadcast events. They will have no effect (except these: Implicit Broadcast Exceptions) Change the registration for them to use Context.registerReceiver()
  • If you have background services in your app, try to replace them with JobScheduler or FCM push notifications
  • If you use notifications, make sure you have at least one NotificationChannel. Otherwise, they won’t show up
  • Make sure changing Settings.Secure.DEVICE_ID to unique per app, not per device, is ok for your application

But, for now, relax – you have still plenty of time, because the official Android O release should be any sooner than July!

 

timeline

Photos source: Android Developer Preview

 

About the author

Paulina Szklarska

Paulina Szklarska

Android Developer

Android Developer @ DroidsOnRoids