HANDLE BACKGROUND SERVICE LIMITATIONS OF OREO

With the introduction of Android 8 (Oreo), we get to know some new and interesting features and behaviour changes of the Android new OS. One of them is “Background execution limits” and in this post, we look specifically at this new restriction that has been introduced for background services. Due to this behaviour change now apps that are running in the background now have limitations on how to access background services and the broadcasts that are defined in Manifest. So if you’re using background services in your app then this change could affect your app. So now let’s have a look at what the actual change is, how it will affect the development and how we can handle it.

Reason behind the Limitation

This behaviour change is introduced to improve the battery life and the usage of RAM when apps are in the background, which can result in better user experiences. Moreover, this change is introduced because background services are also unfavourable for application performance. When more than one app is performing any task in the background then it will affect the performance of the application which is running in the foreground. Also if some additional apps or services are running in background puts extra load on the device system which again affects UI.

How does it Effects on Android App development?

There could be so many reasons we are using background services or broadcasts like to keep your data updated as per the server changes or UI changes on the particular event or may be for performing some long-running task. For whatever reason we are using background task it will be affected by development. Overcoming this limitation will be majorly helpful for client application development for any Android app development company.

What are the Changes?

  • The ‘startService()’ method now throws an ‘IllegalStateException’. If we try to use this method in the case of background services it will not allow.
  • For starting foreground service we now have the ‘Context.startForegroundService()’ method. This method can be called even when the app is in the background but the app must call that service’s method ‘startForeground()’ within five seconds after the service is created.

How to Handle Limitation?

There are three ways to overcome from this limitation, namely –

  1. Job Scheduler
  2. FCM
  3. Foreground Service

Now let’s discuss each in detail below:

1. Job Scheduler

Job Scheduler is introduced in API 21 for performing background tasks. This allows us to move away from background service implementation and just to focus on execution. Using this we can run scheduled services and the Android system will set all the services from different apps in one group or set and execute them together at a particular time. The reason behind this is to reduce the amount of time the phone’s CPU and radio wakes up by batching the tasks together. This will consume less battery. The only drawback of Job Scheduler is it can only be run API 21 or above.

Advantages:

  • Once the device is connected to a power supply, the task should be done.
  • Tasks that require network availability or a Wi-Fi reachability.
  • The task that is not user-facing or critical.
  • Where the timing is not critical the Tasks should be running on a regular basis as the batch

Implementation:
To implement a Job, extend the JobService class and implement the onStartJob and onStopJob. If the job scheduler fails for some reason, return true from on the onStopJob to restart the job. The method onStartJob is performed in the main thread, if you start asynchronous processing in this method, return True otherwise False.

The new JobService must be registered in the Android manifest with the BIND_JOB_SERVICE permission.

And now, below is the snippet to schedule the job.

ComponentName serviceComponent = new ComponentName(context,TestJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0,serviceComponent);
builder.setMinimumLatency(1 * 1000);
builder.setOverrideDeadline(3 * 1000);
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
jobScheduler.schedule(builder.build());

2. FCM

Here to overcome the drawback of Job Scheduler we can use FCM, FCM is supported from API Level 19.

When services are running in the background there are some scenarios which allow applications to temporarily run in the background as if they were running in the foreground. Applications will be included on these scenarios in situations when they are:

  • To trigger high priority FCM/GCM notification
  • To perform SMS/MMS delivery
  • Capture Notification Action

We can send FCM notifications to trigger a service of our application. For this, using High Priority messages allows our application to be added to a service in those scenarios which allows our services to run as if they were in the foreground because High Priority message will be received even when the system is in Doze Mode so by doing this we can start our service to update our application’s data if our application was not running in the background or killed. In FCM we must remember to set messages to High Priority otherwise messages will be received when the device screen is turned back on or during the Doze maintenance window.

3. Foreground Services:

Another simple way is to make service foreground. Sometimes running a service carrying out the task which may require user interaction or monitoring the task which is being executed. Like users downloading some data, using a timer to perform some time-based operation or receiving navigational directions from your application, these are the cases where we can use foreground service. When running foreground services use a persistent notification to acknowledge the user aware that they are currently running.

Implementation:
Before Android Oreo, if you wish to create a foreground service, you mostly start a background service by calling startService(). Then we can start our foreground service by assigning an ongoing notification using the startForeground() method. But from Android Orio startService() will not be working nowadays, so for starting foreground service we must use startForegroundService() method, this method is static and belongs to the class NotificationManager. This method is the same as creating background service and promoting it to the foreground service combine. The method itself is called startService(), but with the contract that startForeground () will be called. The main difference with using this method as compared to startService () is that we can call it at any time, even if our application is not currently in the foreground.

Using the startForegroundService() method, passing an intent for our service task to be carried out. These tricks will create our background service that we must immediately promote to the foreground. Within use of this service, we need to create a notification to be displayed for our foreground service. This must be of at least higher priority or Low so that is shown to the user on screen — if the priority is set to PRIORITY_MIN then the notification will not be displayed to the user. And then, android app developers can call startForeground(id, notification) from within the service —  this will promote our service to the foreground.

Conclusion:

These limitations applied to the background service might lead to some difficulties in development but these will definitely provide extended battery life and also lower RAM usage. So ultimately it will make your applications smooth and your user happy.

profile-image
Vishal Shah

Vishal Shah has an extensive understanding of multiple application development frameworks and holds an upper hand with newer trends in order to strive and thrive in the dynamic market. He has nurtured his managerial growth in both technical and business aspects and gives his expertise through his blog posts.

Related Service

Know more about Android App Development services

Learn more

Want to Hire Skilled Developers?


    Comments

    • Leave a message...