If you are looking at what exactly limitations are applied in background service in Android in Oreo. You are in the right place. In the previous article, I have explained, What is Service and Types of Service. In this article, I’m gonna explain, what exactly limitations are applied in background service in Android in Oreo.

Understand Background and Foreground service

Before moving forward lets clear some terminology, What is background service what is foreground service? The app said to be in the foreground when the app is visible, that is its activity is visible and the user is interacting with the activity. Such as we open the Facebook application so the Facebook application comes in the foreground now some other criteria is when the application has a service running in the foreground.

So if either of the above-mentioned criteria fails then the application is said to be in the background. Let me explain it with an example, suppose in your device, no one app is recently open mean recent app stack is clear now. Now your friend sends you WhatsApp message. One notification will come in WhatsApp mean, WhatsApp application is acting as a background application that is it has not opened it and it has no visibility now. Now launch this WhatsApp application then you get landing right so when the application becomes visible then the WhatsApp application is said to be in the foreground state. In simple words, we can say that when the application is not visible it is in background and also when the application is visible it is said to be in the foreground

Limitation on Background Service

Starting from Android Oreo( API 26) and onwards the background applications(when an application is not foreground ) cannot use the started service. When you call startService() method from the background applications simply through the IllegalStateException. In other words, you can say if you call startService() when your application is not in the foreground.

Why so limitation?  Just because running a service in the background consumes a lot of memory. this simply impacts the device performance thus resulting in that make the quick battery drain and degrade the User experience.

The following service you can run in Oreo (API 26)

  • Bounded Service
  • Foreground Service
  • When the application is currently in the foreground

In case you call startService() method while app is foreground after that application moves to the background, after small amount of time service also shutdown

For better clarity, We will take an example of IntentService and apply below use cases.

Use Case

Now I will create an IntentService, will run it in the foreground as well as background and run also Pre Oreo and Oreo device also.  

Create a new application

Let’s create a new application with Empty Activity template.

Create a subclass of IntentService and add block of code
package com.wave.backgroundserviceexample;

import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

public class CounterService extends IntentService {
    final Handler mHandler = new Handler();
    private static final String TAG = "CounterService";

     * provide name of worker thread
    public CounterService() {

    public void onCreate() {
        showToast("Job Execution Started");

    protected void onHandleIntent(@Nullable Intent intent) {
        int maxCount = intent.getIntExtra("maxCountValue", -1);
         * Suppose we are performing task 1 to 1000, Each task will takes time 1 sec , So You saw we sleep thread or one second.
        for (int i = 0; i < maxCount; i++) {
            Log.d(TAG, "onHandleWork: The number is: " + i);
            try {
            } catch (InterruptedException e) {

    // Helper for showing tests
    void showToast(final CharSequence text) {
        mHandler.post(new Runnable() {
            public void run() {
                Toast.makeText(CounterService.this, text, Toast.LENGTH_SHORT).show();
After that Open actvitiy_main.xml and add below code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="Start IntentService"
        app:layout_constraintTop_toTopOf="parent" />

Let’s move to MainActivity and add below code
package com.wave.backgroundserviceexample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

    public void onStartIntentService(View view) {
        Intent mIntent = new Intent(MainActivity.this, CounterService.class);
        mIntent.putExtra("maxCountValue", 1000);

Now above code, we have created a JobIntentService In this service we are printing 1 to 1000 no in one-second interval. In MainActivity we expose only onClick methods. In this method, We are starting IntentService with the help of startService() method. Now run this project.

Use Case -1 App Foreground and API level smaller than equal to 25 (Pre Oreo)

Build the project and run in pre oreo devices, and click the Start IntentServce button. Button event will be triggered and service is started. You can verify it with Logcat. Output like below

D/CounterService: Job Execution Started
D/CounterService: onHandleWork: The number is: 0
D/CounterService: onHandleWork: The number is: 1
D/CounterService: onHandleWork: The number is: 2
D/CounterService: onHandleWork: The number is: 3
D/CounterService: onHandleWork: The number is: 4
UseCase -2 App Foreground and API level 26 or onward (Oreo or P)

Run the project in Oreo devices and click on Start IntentServce button. The result as same as UseCase-1.

UseCase -3 App Background and API level smaller than equal to 25 (Pre Oreo)

Now testing this use case we have to start the service when the app is background. For running an app in the background, We have to create a receiver with a BootComplete receiver. create a new class like below

package com.wave.backgroundserviceexample;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootCompleteReceiver extends BroadcastReceiver {
    public void onReceive(Context mContext, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            Intent mIntent = new Intent(mContext, CounterService.class);
            mIntent.putExtra("maxCountValue", 1000);

Now register this recevier in manifest

<receiver android:name=".BootCompleteReceiver">
           <action android:name="android.intent.action.BOOT_COMPLETED" />

Run the project and restart the device. While the device will be booted, onReceive() will call and startService() method will execute. The output result will be expected. Mean IntentService will work perfectly in the pre Oreo device.

UseCase – 4 App Background and API level 26 or onward (Oreo or P).

In this use case run the same project in Oreo devices and above and restart the device. The application has crashed in the Oreo device. that means we cannot start the IntentService from the background application in the Oreo device.

The possible solution for dealing with Service in Oreo and onward

What is Service

When none of applications is active state than few Android components are being active in the background and that active component is actually the service. In other words, Service is an application component that executes task in the background without affecting user’s current activity. By default, Service always runs in the main UI thread, It does not runs in separate worker thread and also it is not run in a separate process.

Let’s take some example for better understand. Suppose a user is playing his game, This game is basically activity that shows the current interaction of the user and sometimes while playing the game in the background there are some operations that are going on.

For example while user is busy playing the game, there are few applications are getting updated from Play Store and we all have faced this situation where while interacting with any application in the background all of a sudden the application starts to get updated from the Google Play Store. While the other applications are being updated in the background that time operation is not affecting the current user interaction of playing the game. So all of these background operations is actually carried out by the Android component known as a service

For better clarity let me show you one more example. Suppose you are using the Facebook application and while using the Facebook application there are few background operations that are going on such as uploading or downloading of a file from some other application or while using the Facebook application you want to listen to the music. So here again these two operations are actually being carried out with the help of the Android component known as Service.
Now whenever background operations going on then all the background operations consume some memory. So other words you can say, whenever we use any application or perform any task it definitely eats up your device RAM.

Type of Service

types of service in android

In my opinion the service is broadly categorised into two types the first one is the Started Service and the second one is the Bound Service. The started service can be further categorised into two subcategory the first one can be called as the Foreground Service and the second one is the Background Service .

Now if you have a look at the DEVELOPER.ANDROID that is the official documentation of service then you will find that there are three types of service such as the Foreground, Background, and Bound Service but right now I thought it is much better to categorize the service in 2 subcategories of Started Service and the Bound Service

1. Started Service

Started Services is started with the help of an Android component(Activity, BroadcastReceiver, ContentProvider and Service). That why it is known as a Started Service. The Started service can be started by the Activity, BroadcastReceiver, ContentProvider and the Service itself. Here is noticeable point is that a service can start another service as well as.

The Started Service is starts with the help of a method of startService() method. Whenever any Android component start a Service the Service runs indefinitely in the background and even if the calling component is destroyed then also the started service will continue to run indefinitely in the background. Basically, I am trying to say is suppose the calling Android component is destroyed then also the started service continues to run and this might leads to memory leakage. The started service performs a single operation at a time and there is always a one way communication.

In case of started service that is we can pass the data from the Android component to the service but by default there is no mechanism to get back result from the service back to the calling component so we have to find a workaround to get back result and as said a service continues to run indefinitely in the background that why we should always call stopSelf() method or stopService() method to stop the service and avoid any memory leakage.

Let’s summarise Started Service in short
  • It started by Android components, these components are Activity, ContentProvider, BroadcastReceiver and Service itself.
  • It stared with help of startService() methods and stop by stopService() or stopSelf() methods.
  • When service once started its run in the background indefinitely even when the stared component is also destroyed.
  • By default it has one-way communication only.
  • We should stop the service after the task is finished by using stopService() or stopSelf() methods

2. Bound Service

The service is called a bound service when Android component simply bind to the particular service. We can bind service with three component. The components are Activity, ContentProvider and the Service itself. BroadcastReceiver can never bind the service. and binding to the service is achieved with the help of a special method of bindService() methods. A bind service exist as long as there is at least one component bound to it.

When this component is destroyed then the bound service is also destroyed simultaneously. So we can say that when all the calling components are destroyed then the service is also destroyed. In other words you can say, The Bound Service is dependent on the bounded component for its (own) existence. The bound service has a two way communication Unlike the started service where we had only one way communication so here in case of bound service we have the two way communication you can pass data from the component to the service and also by default get back result.

Summery of Bound Service
  • We can bind to the service with Activity, Content Provider and Service components with help of the bindService() method.
  • When all calling components are destroyed, the service also destroyed.
  • Bound Service continuously interacts with calling components.
  • By default is have two way communication.
1.1 What is Foreground Service

Suppose you are currently using the Gmail application and listening to the music that is being played by some other application. So music player is basically using the foreground service to play the music. The foreground service always uses the notification to notify the user that some long running operation is going on and using the notification you can actually interact with the service or the ongoing operation such as pause the music or play the next music.

So whenever in your application you see a notification that is performing some long running tasks that that service is basically the foreground service and the foreground service is always noticeable to the user that is the user is aware of this ongoing process.

Foreground Service is actually created by using the startForeground() method. So when we use this method it actually start the service to run in the foreground. And by using the stopForeground() method the service again returns back to run in the background and now its becoming the background service now.

I point is noticeable here that using stopForeground() method foreground service is not destroyed. Basically, it actually makes a foreground service to background service. So how to destroy the foreground service so for, that again you need to call the stopService() or stopSelf() the method to destroy the foreground service as well.

When a service runs in the foreground it should always be noticeable by the user to always use notification for the ongoing operation like we saw in case of music player application or downloading or uploading of the file or while updating the applications from the PlayStore we also have the notification to notify the user that some operation is going on.

1.2 What is background service

I will take example of the WhatsApp application and also the Gmail application by taking these two examples you can actually to correlated the things with the Facebook application as well.

Let take a example

Suppose you are connected to internet, Now you will simply turn off your Wi-Fi right. In case you have no internet on your phone. Means you are offline. Now let try to send some message to your friends WhatsApp account. So here you will just try to send a message like (Test message). After sending the message you are not connected to the internet that why this message is currently undelivered and it is showing a pending status close the application and let also destroy the WhatsApp application from the minimized application list. So right now I don’t have any running application and no recent items in the recent app.

Now just turn on Wi-Fi. Now device is connected with internet and message is automatically delivered without WhatsApp app open. In other words WhatsApp application is still closed you have not opened my WhatsApp application but still, my message was delivered to your friend’s device. how is that possible? This is simply because of background service. The WhatsApp application was actually running the background service and due to that service, my message was delivered in the background itself so we don’t need to open the WhatsApp application for our message to get delivered so this is the beauty of using the background service.


A service is started it is actually background in nature so when we use in the startService() method it actually creates a background service by default. Now starting from Android Oreo that is API 26 onwards there are certain restrictions imposed on the background service.

I will discuss it in my upcoming articles so please don’t worry about it and next to the background service is not noticeable by the user as we saw in case of WhatsApp and Gmail how they use the background service to send data to some other user in the background.