How did you decide on implementing a “Background Task In Android”

Select the right API for implementing a “Background Task” in the Android Process

Narendra Harny
Make Android


Photo by Tom Briskey on Unsplash

Android OS provides a lot of freedom to implement applications and services to run on it. Yes, it is a programmer’s choice how they want to code their process which is going run on Android’s Home.

This is exactly like as a citizen of any country, one is equally responsible as a government to make the country clean. So it’s moreover not the individual’s choice whereas it is a responsibility that someone follows the rules to keep the public places clean and possibly there may be some fine if you make a mistake even unintentionally. What kind of fine in both? maybe money when not using public resources correctly.

Hahaha!! same in programming, the system resource is damaged when not writing efficient code!! Jokes apart let’s get back to the technicality of the topic!!

A bad choice of APIs leads to eating the resources making them damaged(battery draining, memory killing) and then affecting the system as a whole.

First of all, we should understand for which condition we are focused and we need to decide choosing the suitable API to implement the background work.

So let’s define our focused Term →

If any activity of an app is not in a visible state and the app currently not running any foreground service. “It means the app is running in the background”

As we know there are many restrictions on Android Applications when it runs in the background. To select the right API we need to again redefine the work which is going to be done in the background. We will just keep defining our requirements until we reach the right API.

What is True in your case is below!!

if (A Real User initiated the task? is True 
&& The app is Visible to the User? is True)
//-----> Go with the first flow chart...
else if (If the Task is a result of any other event is True)
//-----> Go with the second flow chart...

Why we have differentiated the task nature based on these two categories? It is a valid question to ask.

So the important point is for the first case I said “The app is Visible to the User” which means the app component initiates the task is already running on the highest priority and also User may be willing to interact with the UI without any interrupts.

The second point is “A task may be initiated by any other external event like a notification or broadcast” which certainly differs from the first case because the user’s UI interaction state is undefined in this case.

So once we define our Task as per the above category let’s move further breaking into each! So we are defining three categories of works based on their nature which is the outcomes for us.

To know Whether our task is →

Asynchronous work?

Background work?

Foreground services work?

The above state is the finalizing state where we want to reach.

A Real User initiated the task? True

The above decision tree shows that the task that needs to be implemented comes under which category.

  1. Asynchronous Task
  2. Background Task
  3. Short-Term Task Service
  4. Foreground Service.

According to the nature of the task, we can categorize the task and find out the best-suited API for its implementations.

The task is a result of any other event? True

As explained in the above decision tree here also the same decision needs to be taken and find out the most suitable API for the task execution.

The conclusion!

For example, Background Task is a broader term and Asynchronous Task comes under a type of background task with some special requirements that say if the Task is Very Short and can be finished in a second or two then we can have a specific API which is good for Asynchronous task and not good for a long-running background task.

End of the story!!

Did you precited selecting the best Data Structure for your data storage while utilizing the precious RAM? If not this topic is also saying the same thing in the context of background work in the Android Process. It reminds me of a very basic Java collection framework interview question that is.

“What are the differences between Array List and Link List”

The answer in short is More Searching → Array List is best For More Manipulation Linked List is Best You already know, that there are so many questions that will come out of this one question choosing between an Array List and a Linked List.

We can understand the importance of choosing the right data structure for a small module of an application then choosing the right API for performing background Work in the Android Process is way more important.


Thanks for Reading!

Please comment with questions and suggestions!! If this blog is helpful for you then hit Please hit the clap! Follow on Medium, Connect on LinkedIn, Follow on Insta and send emails if any discussion is needed on the same topic on copy email.
Please Follow & subscribe to Make Android Publication by Narendra K H for prompt updates on Android platform-related blogs.

Thank you!



Narendra Harny
Make Android

Connect on | Write On, Android AOSP & Applications | Python | DevOps | Java | C++ | Kotlin | Shell | Linux | Android Auto | IVI