In Android development, the Work Manager is a powerful, flexible background processing library that allows you to schedule and manage deferrable, asynchronous tasks. It is part of the Android Jetpack library and provides a way to perform background tasks that should run even if the app is not actively running.
- Schedule tasks that can run even if the app exits or the device restarts.
2. WorkManager helps execute long-running deferrable scheduler tasks.
3. WorkManager is a library for persistent work.
//WorkManager
def work_version = "2.9.0"
implementation "androidx.work:work-runtime:$work_version"
NotificationWorker.java
package com.demoworkmanager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
public class NotificationWorker extends Worker {
public NotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
// Show a simple notification
showNotification("WorkManager Notification", "This is a WorkManager notification.");
// Return success
return Result.success();
}
private void showNotification(String title, String message) {
NotificationManager notificationManager =
(NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(
"work_manager_channel",
"Work Manager Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
notificationManager.createNotificationChannel(channel);
// Build the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), "work_manager_channel")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
// Show the notification
notificationManager.notify(1, builder.build());
}
}
MainActivity.java
//For OneTime Request
// Enqueue the work request with the WorkManager
OneTimeWorkRequest oneTimeWorkRequest = new OneTimeWorkRequest.Builder(NotificationWorker.class).build();
WorkManager.getInstance(getApplicationContext()).enqueue(oneTimeWorkRequest);
//For Repeat Interval Request
// Enqueue the periodic work request with the WorkManager
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(NotificationWorker.class,
1, // repeat interval in minutes, at least 1 minutes
TimeUnit.MINUTES).build();
WorkManager.getInstance(getApplicationContext()).enqueue(periodicWorkRequest);
#KotlinProgramming #AndroidDevLife #SoftwareEngineering #GradleBuilds #AndroidApps #KotlinCoder #DeveloperLife #GoogleAndroidDev #MediumArticles #KotlinLove #AndroidStudio #SoftwareDevelopmentLifeCycle #LinkedInTips #KotlinCommunity #AndroidCoding #GradleConfig #MediumWriters #DeveloperCommunity #KotlinCode #AndroidSDK #SoftwareEngineers #GradleDependencies #MediumWriting #AndroidDevelopmentTips #KotlinExperts #SoftwareDeveloper #LinkedInForDevelopers #KotlinApps #AndroidLife #GoogleDevelopersGroup #GradlePlugin