Open In App

How to Integrate Work Manager in Android?

Last Updated : 10 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In our day-to-day life, we are using apps like alarms, tasks reminders on our phones. Earlier running background tasks was a very tough task in Android but now with the help of Work Manager, we can schedule our tasks easily. Work Manager is a library of Android Jetpack. It allows the app to do things in the background even when the app is exited or the device is restarted. In this article, we will learn how to integrate the work manager in your Android App which makes our job easy in scheduling tasks & reminders.

Steps by Step Implementation

Step 1: Create a new Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Add dependencies in your grade files.

Navigate to the Gradle Scripts > build.gradle(Module: app) and add the below codes.

implementation("androidx.work:work-runtime-ktx:2.7.0")

Step 3: Create a Worker Class

Create a class and then extend Worker Class on that class and override its doWork() method. Whatever tasks you want to perform on background write that on the doWork() method. It is responsible to execute that task.

Kotlin




import android.content.Context
import android.util.Log
import androidx.work.Worker
import androidx.work.WorkerParameters
  
class WorkerClass(appContext: Context, workerParams: WorkerParameters): Worker(appContext, workerParams) {
  
    override fun doWork(): Result {
  
        // Enter work details to perform on background
          // Log.d is used to for debugging purposes
        Log.d("WorkerClass","It's Working")
  
        // Task result
        return Result.success()
    }
}


The Result returned from doWork() informs the WorkManager whether the work succeeded and, in the case of failure, whether or not the work should be retried. These are Result.success(), Result.failure(), Result.retry().

Step 4: Working with the MainActivity.kt file

In our MainActivity file, we will create a work request. Add the following code to your file by replacing the names of the class and Activity.

Kotlin




package com.mrtechy.gfg_workmanager
  
import WorkerClass
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.WorkRequest
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Created a Work Request
        val uploadWorkRequest: WorkRequest = OneTimeWorkRequestBuilder<WorkerClass>().build()
  
        // Submit the WorkRequest to the system
        WorkManager.getInstance(this).enqueue(uploadWorkRequest)
  
    }
}


Output: We have used Log.d to show output in the logcat window.

Logcat Output:

logcat

App Output:

output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads