5

I have this piece of code that fire the alarm once by setting a time and date using the TimePicker and the DatePicker in another activity. I want to modify it in a way that whenever I set a time and a date it will fire the alarm everyday at the same time. In other words I want the alarm to be fired daily.

public class M_ReminderManager {

    private Context mContext; 
    private AlarmManager mAlarmManager;

    public M_ReminderManager(Context context) {
        mContext = context; 
        mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    }

    public void setReminder(Long reminderId, Calendar when) {

        Intent i = new Intent(mContext, Medicines_OnAlarmReceiver.class);
        i.putExtra(RemindersDbAdapter.KEY_ROWID_MEDS, (long)reminderId); 

        PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT); 
        mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
      }
}

I have tried using setRepeating function but I don't know how exactly I should set the attributes I used this line instead of the set function on the code but it didn't work:

mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis() ,AlarmManager.INTERVAL_DAY , pi);

Can someone help me with it?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
zoza
  • 127
  • 3
  • 12

2 Answers2

4

Just modify the code

alarmManager.set(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(), pendingIntent)

in AndroidAlarmService class to

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(), 5*1000, pendingIntent)

And read this link: http://android-er.blogspot.com/2010/10/schedule-repeating-alarm.html

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
  • 2
    you need to replace 5*1000 with AlarmManager.INTERVAL_DAY to set it daily – Udi Oshi Jan 02 '13 at 06:33
  • The method set(int, long, PendingIntent) in the type AlarmManager is not applicable for the arguments (int, long, long, PendingIntent) – Prasad Nov 04 '15 at 14:12
1

You can see this link: How to run a service every day at noon, and on every boot You should remember to reset schedule on phone restart, link includes code for that aswell.

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155