GVillani82
GVillani82

Reputation: 17429

Android: scheduling event for remainder

In order to implement a reminder, I need to set a memo like: "starting from today, show a memo, each friday, one week yes and one no" So, I think I can determin for each memo, what is the next date I have to show it. And then pass this date to a Timer like this:

 Timer timer = new Timer();

 timer.schedule(new TimerTask() {

                public void run() {
                    //show my memo    
                     });

                }}, date); // 

Supposing now is Mon 26/11/2012 how can I determine when occurs the first friday (according to the aforementioned memo?

I'm not sure this mechanism is enough good, if someone can suggest me another approch I would be grateful.

Upvotes: 1

Views: 288

Answers (1)

Bruno Oliveira
Bruno Oliveira

Reputation: 5076

Timer is not a good class to use for this. You should take a look at AlarmManager and schedule your events using that.

You can use AlarmManager.set() to set a specific alarm, AlarmManager.setInexactRepeating() to set a repeating alarm that has some flexibility in terms of exactness, setRepeating() to set a precise repeating alarm.

In all cases you set up a PendingIntent that gets launched when the alarm fires, and your application should be prepared to handle that intent correctly.

Remember that you don't necessarily need to take action when that PendingIntent is fired: you can just check if the conditions are right (for example, you can add some logic as to whether the user should be notified or not at that point).

More about AlarmManager and PendingIntent:

http://developer.android.com/reference/android/app/AlarmManager.html

http://developer.android.com/reference/android/app/PendingIntent.html

Also, remember that you have to add a broadcast receiver to the "boot sequence completed" event so that you can reinstall your alarms after the device has been rebooted, since alarms don't persist across reboots.

Upvotes: 3

Related Questions