2

How to create a AlarmManger which can be invoke on fixed date and time, This can be also repeat continuous by nature

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85

2 Answers2

5

unfortunately any of the options on the AlarmManager for repeating tasks doesn't allow such a fine control. Your best approach is to on every alarm, you re-schedule it for the next month.

PendingIntent pendingIntent = // set here your action
Calendar calendar = // set this guy to be the next 5th day
AlarmManager am = // get reference to the manager
am.set(RTC, calendar.getTimeInMillis(), pendingIntent);

on inside this Pending intent action you repeat the code. For example, let's say you want to launch a BroadcastReceiver

onReceive(Context context, Intent intent){
    Calendar calendar = // set this guy to be the next 5th day
    AlarmManager am = // get reference to the manager
    am.set(RTC, calendar.getTimeInMillis(), pendingIntent);    
}

to set the calendar object is easy:

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY, 5);
Budius
  • 39,391
  • 16
  • 102
  • 144
  • Thanks for replying : Actually my application some-kind of background application and, it have task for every date of month repetitive, unfortunately device is manged remotely by GCM so there is no UI. – Lavekush Agrawal Jun 24 '14 at 12:23
  • well... if you have a different task for every day of the month, why not simply set the AlarmManager to repeat once a day, and inside the broadcast or service do a `switch case` for the day – Budius Jun 24 '14 at 12:38
1

I just read a good answer to do the same.

The code is-

Calendar cal=Calendar.getInstance();
cal.set(Calendar.MONTH,5);
cal.set(Calendar.YEAR,2012);
cal.set(Calendar.DAY_OF_MONTH,11);

cal.set(Calendar.HOUR_OF_DAY,16);
cal.set(Calendar.MINUTE,10);
cal.set(Calendar.SECOND,0);

Intent _myIntent = new Intent(getApplicationContext(), ReceiverClass.class);

PendingIntent _myPendingIntent = PendingIntent.
getBroadcast(getApplicationContext(), 123, 
_myIntent, PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);

AlarmManager _myAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

//_myAlarmManager.set(AlarmManager.RTC_WAKEUP, 
System.currentTimeMillis() + (10 * 1000), _myPendingIntent);
_myAlarmManager.set(AlarmManager.RTC_WAKEUP, 
cal.getTimeInMillis(), _myPendingIntent); 

This is explained in android-alarm-setting-with-specific-date.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185