AlarmManager Vs WorkManager Android Background task processing

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.

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


Thanks 
Saroj Kumar Malik

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...