CategoriesBackground Task

Guide to background Task handling – Part 5 (AlarmManger)

If you have been following this series you may have noticed already we haven’t found any Andoird component that can handle a background task to execute at the exact given time. The closest we come to achieving something like that is by using PeriodicWorkRequestBuilder in WorkManager . Even when using WorkManager that it won’t wake your phone from Doze mode. AlarmManager is the suitable component that can be used to execute a background task in a fixed time in the future.

How does AlarmManager work?

AlarmManager allows developers to schedule tasks that must be executed at a specific time or after a specific interval. It is a system service that runs in the background and handles all the scheduled tasks. When an alarm is triggered, the AlarmManager sends a broadcast intent to the system, which starts the specified service or activity. The AlarmManager Itself is not capable of running a background task but rather capable of triggering a component that is capable of executing a background task. commonly uses Service with AlarmManager for executing background tasks.

One of the most significant advantages of using AlarmManager is that it allows developers to schedule tasks even when the application is not running. The only downside is that AlarmManager will get reset if the device is restarted. But there is a workaround for that which we will discuss in this article.

Continue reading
CategoriesBackground Task

Guide to background Task handling – Part 4 (WorkManager)

WorkManager is an Android component for handling background tasks that need to schedule and survive app restarts and system reboots. WorkManager also can be used for executing one-time background tasks which replaced the functionality of now deprecated IntentService and JobIntentService . WorkManger can handle Immediate, Long running and Deferrable types of background workloads.

WorkManger is a feature pack component compared to other background tasks handling components we discussed so far. Let’s look at the WorkManger features.

Robust scheduling

As I mentioned above briefly work-manager can handle all three types of background tasks. When it comes to scheduling a unique tag and name can be given to a worker to replace, restart, cancel or monitor it. Work-manger schedule data is stored in SQLite database which allows itself to run persistently. In addition to that work manager build in mind with power-saving features so as developers we do not need to worry about it.

Continue reading
CategoriesBackground Task

Guide to background Task handling – Part 3 (Coroutines)

In this article, we will explore the concept of coroutines as a means of handling background tasks in our applications. Coroutines are the go-to solution when it comes to handling Immediate impersistent tasks. A lot of Android libraries come with coroutines support out of the box making it easier to perform those libraries’ functionalities incorporated with coroutines.

Coroutines are a type of function that allows you to pause the execution of a function, perform another task, and then return to the paused function to continue execution from where it left off. This makes them ideal for handling asynchronous tasks such as network calls, database operations, and other long-running tasks.

Advantages of Coroutines

  • Simplicity: Coroutines are easy to use and implement compared to other concurrency models such as threads and reactiveX.
  • Efficiency: Coroutines are lightweight and have less overhead compared to threads, making them more efficient when handling a large number of tasks.
  • Readability: Coroutines use a sequential style of coding, making them more readable and easier to understand than callback-based coding.

let’s try to break down each of the concepts in the Coroutines.

Continue reading
CategoriesBackground Task

Guide to background Task handling – Part 2 (Services)

In this article, we are going to look at Android services. Service is best used when there is a task that needs to be executed in the background without user interaction. However, there are service types that users can interact with. Usually, services are used for implementing long-running tasks.

Types of Services

There are three types of services to choose from based on the nature of the task.

Foreground Service

When we need an application user to notice a background task is processing we use this service type. This service can survive even after the application is destroyed. Users will see a notification when this type of service is running in the background. For example, downloading large files would use a foreground service to handle that task.

Background Service

When there is a background task that needs to implement and it is not necessary to inform the user we can use background service. For example, Performing a cashing mechanism in which the user doesn’t need to know a background service can handle that task.

Bind service

When there is a task that needs to be handled in the background with user interaction a bind service can be used. This interaction can be a form of sending result data back to the UI layer or sending a request to the service to perform an additional task from the UI layer. A bound service runs only as long as another application component is bound to it.

Continue reading
CategoriesBackground Task

Guide to background Task handling – Part 1 (Threads, Runnable and Handler)

In Android development, being able to handle tasks in the appropriate thread is an essential skill to have. In this series, I’m planning to cover all the available methods in Android for handling tasks in the background. As the first article of this series, I will explain the basics of background tasks and will explain how to handle a background task using a thread.

What is a background task?

The simplest answer is any task that needs to execute outside the main thread is qualified as a background task. But there are many variations of background tasks. it can be in the range of doing small network calls in the background to doing background tasks even though the application is closed. The following graph summarises the range of background tasks that we might need to implement during our Android application development.

Continue reading