How to Handle Android Background Services?

137 Views
Android-Background-Service

I decided to write this article after a long struggle in executing background tasks in android which took such a long time to find the way out to manage background tasks. 

I have tried many ways to find this out and hence trying to put it together to discuss my findings so as to help any other android developer who might be stuck in the same problem too. 

A background service performs an operation that isn’t directly interfered by the user. For example, if an app used a service to compact its storage, that would usually be a background service

Methods of executing background tasks

  1.   Services

        A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface.

       Another application component can start a service, and it continues to run in the background even if the user switches to another application.

        a. Foreground

                  For example, an audio app would use a foreground service to play an audio track.Foreground services must display a status bar icon. Foreground services continue running even when the user isn’t interacting with the app.

          b. Background

                  If an app used a service to compact its storage, that would usually be a background service.                   

  1. Notifications

       A notification is a message our app displays to the user outside our application’s normal UI.When we tell the system to issue a notification, the notification first appears to the user as an icon in the notification area, on the left side of the status bar.

       — Starting in Android 8.0 (API level 26), notification channels allow us to create a user-customizable channel for each type of notification we want to display

  1. Job Scheduler

       Schedule background tasks expected to run even if the app exits or device restarts. Work Manager API makes it easy to schedule asynchronous tasks. 

  1. Broadcast Receiver

        Android apps can send or receive broadcast messages from the Android system and other Android apps, similar to the publish-subscribe design pattern.

       For example, the Android system sends broadcasts when various system events occur, such as when the system boots up or the device starts charging.

       Apps can also send custom broadcasts, for example, to notify other apps of something that they might be interested in.

Challenges of background service

From Marshmallow onwards, Android has introduced two new feature called Doze Mode and Standby Mode. This will give some more life to your phone i.e. extend its battery.      

  1. Doze Mode

                  –  If the user leaves the device unplugged for a period of time and the screen is off, then the device will enter into Doze Mode. In Doze mode, the system attempts to conserve battery by restricting apps from accessing the network and intensive CPU utilization services. It also prevents the app from accessing the networks and defer their jobs, syncs, and alarms.                

  1. Standby Mode

                  – The system will enter into standby mode if the user is not actively using the app for a period of time and also there is no process for the app which is currently in the foreground (either as an activity or foreground service).

      How to solve Doze Mode

                  – Here we will see how we can create an application and will send some pieces of information to the backend API in every 2 minutes regardless of whether the app is running or not.


I hope this information related to the know-hows on the Android Background Service will guide and be of help to you. Go ahead and share your thoughts too in the comment box and I would be happy to share more.

137 Views

Leave a Reply