Work Manager, the new Advanced Alarm Manager | Supports API 14+ | Works even in Doze Mode

Google has already developed an advanced version of Alarm Manager which has much more modern functionality and works in a battery-efficient manner.

Since Android Oreo and above, Google has introduced doze mode which suspends almost all the Alarm Manager requests. This keeps the alarms from ringing until the mobile wakes up from doze mode. Many developers have complained a lot about this functionality been jeoparding the normal behavior of their apps. Some have found workarounds, while some have limited the background work in their application but most of the developers don't know that Google has already developed an advanced version of Alarm Manager which has much more modern functionality and works in a battery-efficient manner. 

Work Manager
Credits: https://developer.android.com/


What is Work Manager?

Work manager is Google's new API which schedules asynchronous tasks and does the background work in a very efficient way even when device restarts or app exits. Work Manager is backwards compatible till Android 4.4 where it uses Alarm Manager and from Android Oreo onwards it uses job scheduler to dispatch the task. 

Types of Work:

There are two types of work: One-time work and periodic repeating work. One time work, as the name suggests, is a one time request which needs to be performed on the background thread. Periodic Work, on the other hand, is used to perform repeating periodic tasks in the background even after the application has exited long back.

How to use Work Manager:

Add the dependency for Work Manager :
[Java Version
implementation "androidx.work:work-runtime:2.4.0"
Kotlin Version
implementation "androidx.work:work-runtime-ktx:2.4.0"]

 To create a periodic work request, add the following lines:

[WorkRequest uploadWorkRequest = new PeriodicWorkRequest.Builder(NotificationWorker.class, 1, TimeUnit.DAYS).build();
WorkManager.getInstance(c).enqueue(uploadWorkRequest);]

Now write the background task, you want to perform here:

[public class NotificationWorker extends Worker {
private static Context context;
public NotificationWorker(
@NonNull Context context,
@NonNull WorkerParameters params) {
super(context, params);
this.context = context;
}
@Override
public Result doWork() {
try {
//TODO YOUR PREFERRED BACKGROUND TASK
return Result.success();
} catch (Exception e) {
e.printStackTrace();
return Result.failure();
}
}
}] 

 Thats it. You have successfully implemented periodic work requests using the Work Manager. 

To schedule periodic Work Manager tasks with Alarm Manager, instead of your usual Alarm Manager code, write this code :

[alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeinmillis,notifyPendingIntent);]

This line will wake Alarm Manager even when it is in doze mode and will work without any problems in Android Oreo and above versions.

COMMENTS

BLOGGER
Name

Affiliate marketing tutorial,2,Android,60,Android Development,22,Android tutorials,41,Blogging tactics,8,Deals,1,DSA,4,Elite Youtube Tutorial,4,Firebase,1,Flutter,4,Fuchsia,1,Hosting Guides,3,Leetcode,4,Miscellaneous,10,Mobile Gaming,17,NTN,94,Problogging,7,PUBG,16,Reviews,1,SEO Tutorial,5,Start a blog,9,Start a channel,6,Top 5,4,Travel Blogging,4,Windows,12,Windows Security,4,Windows tutorials,7,Wordpress Tutorials,4,Youtube Tutorials,11,
ltr
item
A Passionate Developers Creation: Work Manager, the new Advanced Alarm Manager | Supports API 14+ | Works even in Doze Mode
Work Manager, the new Advanced Alarm Manager | Supports API 14+ | Works even in Doze Mode
Google has already developed an advanced version of Alarm Manager which has much more modern functionality and works in a battery-efficient manner.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBbrOozpJDz0JXgcMPmIPF6-aVhthCZXUTBZUG7C_RTUoWmzuYz4hthhOesdQJndFPsR0c0IK6apw12lzVrnpxHQ7XZ_VyCV_bVDbvT7yN0_aUiJvQd5tP5JJNjTaiP9Ya52gO8hGPxP0/w320-h152/Work+Manager.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBbrOozpJDz0JXgcMPmIPF6-aVhthCZXUTBZUG7C_RTUoWmzuYz4hthhOesdQJndFPsR0c0IK6apw12lzVrnpxHQ7XZ_VyCV_bVDbvT7yN0_aUiJvQd5tP5JJNjTaiP9Ya52gO8hGPxP0/s72-w320-c-h152/Work+Manager.jpg
A Passionate Developers Creation
https://www.apdevc.com/2020/09/work-manager-new-advanced-alarm-manager.html
https://www.apdevc.com/
https://www.apdevc.com/
https://www.apdevc.com/2020/09/work-manager-new-advanced-alarm-manager.html
true
3690156770086819421
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content