Sunday 24 November 2019

AlarmManager Vs WorkManager Android Background task processing

 Hi Guys!!!

Today topic is " background processing in Android" . There is a lot of things added in Android from initial day to till now to perform background processing in android efficiently.

Today i am going to focus on Alarm Manager and Work Manager API in Android. You can find my example source code from my github page.

As we know every Android app has a main thread which is in charge of handling UI (including measuring and drawing views) , coordinating user interactions, and receiving lifecycle events.

If there is too much work happening on this thread, the app appears to hang or slow down, leading to an undesirable user experience. That's why we need to off load Main UI Thread and perform long operation in background.

When to use WorkManager
  • If the work dependent on system conditions
  • Your job to run only when the device meets certain conditions(such as being connected to power) 
WorkManager mWorkManager = WorkManager.getInstance(MainActivity.this);
PeriodicWorkRequest.Builder myWorkBuilder =
        new PeriodicWorkRequest.Builder(RebootWorker.class, 1, TimeUnit.MINUTES);
PeriodicWorkRequest workRequest = myWorkBuilder.build();
WorkManager mWorkManager.enqueue(workRequest);

When to use AlaramManager


  • Job need to run at a precise time
  • A calendar app might let a user set up a reminder for an event at a specific time

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, uniqueInt, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, pendingIntent);

Choosing the right background Processing for your work
There is lot more  long background task processing is avilable to perform your job as per your requirement. I named few below :-
  • WorkManager
  • Foreground services
  • AlarmManager
  • DownloadManager

Use below decision making daigram to choose right one

refe link:- https://developer.android.com/images/guide/background/bg-job-choose.svg

Thanks 

Happy Coding !!!!
Saurabh Sharma

1 comment:

  1. To manage your time efficiently and smartly, Efficient calendar apps for windows is one of the best schedule app for Windows 10. It uses calendar, tasks and events modules with a responsive interface that displays the features on the left end of the calendar. Keep track of your appointments and events also, and you can have a multiple calendar view. This list view with multiple calendars makes managing all the events quicker without taking much time. It can be easily synced with your mobile phone and PC in just 4 simple steps without any complexity.

    ReplyDelete

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...