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
ScheduledExecutorService
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?
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. OnceonReceive()
returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as youronReceive()
method completes. If your alarm receiver calledContext.startService()
, it is possible that the phone will sleep before the requested service is launched. To prevent this, yourBroadcastReceiver
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:
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.