Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is Better ScheduledExecutorService or AlarmManager in android?

I am a beginner and I am developing an android application which will keep on sending SMS to the user after a certain delay (which is in days).I want that the user once registered should receive the SMS irrespective of the fact that he is logged in or not. The SMS content and mobile number are fetched from the database.So after researching I found two ways

  1. ScheduledExecutorService

  2. AlarmManager

The problem is that the alarmManager will shut down when the phone is switched off or rebooted. Is this the case with ScheduledExecutorService too? And how many threads should I use in the ThreadPool while using the Executor Service?

like image 458
R_2293 Avatar asked May 21 '14 05:05

R_2293


People also ask

How is AlarmManager different from WorkManager?

Unlike WorkManager, AlarmManager wakes a device from Doze mode. It is therefore not efficient in terms of power and resource management.

What is ScheduledExecutorService in Java?

public interface ScheduledExecutorService extends ExecutorService. An ExecutorService that can schedule commands to run after a given delay, or to execute periodically. The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution.


1 Answers

Alarm Manager

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

ScheduledThreadPoolExecutor.

You can use java.util.Timer or ScheduledThreadPoolExecutor (preferred) to schedule an action to occur at regular intervals on a background thread.

Here is a sample using the latter:

ScheduledExecutorService scheduler =
    Executors.newSingleThreadScheduledExecutor();

scheduler.scheduleAtFixedRate
      (new Runnable() {
         public void run() {
            // call service
         }
      }, 0, 10, TimeUnit.MINUTES);

So I preferred ScheduledExecutorService

But if the updates will occur while your application is running, you can use a Timer, as suggested in other answers, or the newer ScheduledThreadPoolExecutor. If your application will update even when it is not running, you should go with the AlarmManager.

The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

Take note that if you plan on updating when your application is turned off, once every ten minutes is quite frequent, and thus possibly a bit too power consuming.

Also check out this post.

like image 182
Rohit Avatar answered Sep 19 '22 12:09

Rohit