Currently I am working on an application to set reminders on monthly basis. I am not able to provide the correct repeating interval for my alarmmanager. Pls provide info about the same. this is my code, but this will not raise alarm for Feb or months having 30 days. Also pls provide code to set yearly repeating alaram.
repeatTime=(AlarmManager.INTERVAL_DAY*31);
mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, when.getTimeInMillis(), repeatTime, pi);
Thanks, Sharath
At the top of the main panel you should see an option to Add alarm. Tap this and you'll be presented with a time in the top half of the screen, with various settings in the lower half. Scroll the hours up or down until you reach the one you want, then repeat the process with the minutes.
All you have to do is go to clock... then look at the plus + sign and beside it is 3 vertical dots click that. Once there unclick the automatic mode and you will be able to set date and time of your alarms! Enjoy! Or set it on your calendar and set the reminders up.
this is how you calculate interval between today in extacly one month after, use this logic to reset alarm everytime once it triggers. i.e set alarm to the point when you want to start, supply some pending intent, once alarm triggers use below code to get next trigger time, and set alarm again to trigger at that time.
private long getDuration(){
// get todays date
Calendar cal = Calendar.getInstance();
// get current month
int currentMonth = cal.get(Calendar.MONTH);
// move month ahead
currentMonth++;
// check if has not exceeded threshold of december
if(currentMonth > Calendar.DECEMBER){
// alright, reset month to jan and forward year by 1 e.g fro 2013 to 2014
currentMonth = Calendar.JANUARY;
// Move year ahead as well
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR)+1);
}
// reset calendar to next month
cal.set(Calendar.MONTH, currentMonth);
// get the maximum possible days in this month
int maximumDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
// set the calendar to maximum day (e.g in case of fEB 28th, or leap 29th)
cal.set(Calendar.DAY_OF_MONTH, maximumDay);
long thenTime = cal.getTimeInMillis(); // this is time one month ahead
return (thenTime); // this is what you set as trigger point time i.e one month after
}
To set alarm which repeat monthly,
Calendar calender= Calendar.getInstance(TimeZone.getDefault());
int cDay = calender.get(Calendar.DAY_OF_MONTH);
calender.set(Calendar.HOUR_OF_DAY, hour); //hour you have selected
calender.set(Calendar.MINUTE, min); //min you have selected
calender.set(Calendar.SECOND, 0);
calender.set(Calendar.MILLISECOND, 0);
calender.set(Calendar.DATE, cDay);
calender.get(Calendar.MONTH);
Calendar now = Calendar.getInstance();
now.set(Calendar.SECOND, 0);
now.set(Calendar.MILLISECOND, 0);
int days = now.getActualMaximum(Calendar.DAY_OF_MONTH);
if (calender.before(now)) { //this condition is used for future alarm only
calender.add(Calendar.DATE, days);
}
final int _id = (int) System.currentTimeMillis();
Intent i = new Intent(activity, YourServiceClass.class);
i.putExtra("type", "month");
PendingIntent displayIntent = PendingIntent.getBroadcast(
activity, _id, i, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), AlarmManager.INTERVAL_DAY * calender.getActualMaximum(Calendar.DAY_OF_MONTH), displayIntent);
Now in your service class , put below code
if (intent.getExtras() != null) {
type = intent.getStringExtra("type");
}
if (type != null) {
if (type.equals("month")) {
Long futureTimeDifference = intent.getLongExtra("futureTimeDifference", 0); // Receive the time difference in milliseconds from currenttime in milliseconds and the future set date milliseconds
futureTimeDifference = futureTimeDifference + System.currentTimeMillis();// get the next schedule date time inmilliseconds
String repeatType = intent.getStringExtra("getRepeatType");// Receive the repeat type
Date todaysDate = new Date();// initialize a new date object
Calendar getCurrentDate = Calendar.getInstance();// Initialize a new Calendar object
getCurrentDate.setTime(todaysDate); //Set the calendar to todays date
int currentMonth = getCurrentDate.get(Calendar.MONTH); // Assign the current month in integer
if (currentMonth == Calendar.JANUARY || currentMonth == Calendar.MARCH || currentMonth == Calendar.MAY || currentMonth == Calendar.JULY || currentMonth == Calendar.AUGUST || currentMonth == Calendar.OCTOBER || currentMonth == Calendar.DECEMBER) {
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 31);
}
if (currentMonth == Calendar.APRIL || currentMonth == Calendar.JUNE || currentMonth == Calendar.SEPTEMBER || currentMonth == Calendar.NOVEMBER) {
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 30);
}
if (currentMonth == Calendar.FEBRUARY) {//for february month)
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
if (cal.isLeapYear(cal.get(Calendar.YEAR))) {//for leap year february month
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 29);
} else { //for non leap year february month
futureTimeDifference = System.currentTimeMillis() + (AlarmManager.INTERVAL_DAY * 28);
}
}
final int monthly_id = (int) System.currentTimeMillis();
Log.e("MonthlyNotification", futureTimeDifference + "");
PendingIntent displayIntent = PendingIntent.getBroadcast(
context, monthly_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, futureTimeDifference, displayIntent);
//Toast.makeText(context, "Notification Set Monthly", Toast.LENGTH_SHORT).show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With