Muhammad Umar
Muhammad Umar

Reputation: 11782

Local Notifications each month in android

I am using following code

AlarmManager service = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP ,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY , pending);

// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);

In my code i hope it works after interval of one day and does a broadcast for AlarmReceiver service.

However i want this to happen after exactly one month. E,g if a guy installs the application on 3rd of Jan the next alarm will occur on 3rd of Feburary and so on. How can i put interval of one month.

Upvotes: 3

Views: 1489

Answers (1)

itsrajesh4uguys
itsrajesh4uguys

Reputation: 4638

Please use the following code. it dont have much difference from your current code.

AlarmManager service = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, AlarmReceiver.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pending = PendingIntent.getBroadcast(this, 0, i,PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    // Start 1 month after boot completed
    cal.add(Calendar.MONTH, 1);
    //
    // Fetch every 1 month
    // InexactRepeating allows Android to optimize the energy consumption
    service.setInexactRepeating(AlarmManager.RTC_WAKEUP ,cal.getTimeInMillis(),AlarmManager.INTERVAL_DAY , pending);

Upvotes: 2

Related Questions