1

I want to have an android alarm manager to runs a service at the first day of every month. I have the folowing code:

     PersianCalendar persianCalendar = new PersianCalendar();
    persianCalendar.setTimeZone(TimeZone.getDefault());
   if( persianCalendar.getPersianDay()==1){

    Intent intent = new Intent(UsersActivity.this, AppsNetUsageReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
           alarmManager.set(AlarmManager.RTC, persianCalendar.getTimeInMillis(), pendingIntent);
}

How can I guarantee my service runs successfully if the device was off or other reason prevent to my service do its work?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amirhosein
  • 4,266
  • 4
  • 22
  • 35

1 Answers1

0

You have to set the next alarm when previous alarm is received.

There is setRepeating(@AlarmType int type, long triggerAtMillis,long intervalMillis, PendingIntent operation) method in AlarmManager which can be used to set repeating alarms like every hour or every 15 min.

But in this case you can't provide intervalMillis since intervals can vary due to different no of days in months.

And as @Ya Si said, there are other ways to set alarms apart from AlarmManager which handles power-off/restarting cases automatically. If you still want to use AlarmManager check : Start AlarmManager if device is rebooted

Gaurav Chauhan
  • 376
  • 3
  • 14