Reputation: 229
I am setting notifications using alarm manager and broadcast receiver. I have tried to use setInexactRepeating method of an alarm manager but it dose not raise alarms at exact time above API 19.
So I got a suggestion to use setExact method and set alarms manually. I don't know how can I do this. Do I need to calculate dates for every week for a year?
How can I set time for the same day every week? Like If I have set time for monday of this week I want to get alarm for every monday for all next week.
How Can I set time for every week's day?
Here is how I set alarm:
public void setNotificationTime(Calendar c)
{
Date dateFrom = new Date();
df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
try {
dateFrom = df.parse(startTime);
}
catch (ParseException ex) {
}
dateFrom.getTime();
c.setTime(dateFrom);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
if(notificationTime.equals("10 Minutes Before"))
{
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 10);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day);
// c.set(Calendar.DAY_OF_WEEK,);
SetDay(c);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
intent = new Intent(getBaseContext(),NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
else if(notificationTime.equals("30 Minutes Before"))
{
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 30);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day);
// c.set(Calendar.DAY_OF_WEEK,);
SetDay(c);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
intent = new Intent(getBaseContext(),NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
}
My attempt is to use for loop to get next weeks date and set the alarm. So for loop will run 52 times i.e. 52 weeks of a year.
It will set dates for every week of a year. But I am only getting next weeks date i.e if today is 11 feb I am getting date as 18 feb. It's not going further. What's wrong here?
public void setDates(Calendar c)
{
Date dateFrom = new Date();
df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
try {
dateFrom = df.parse(startTime);
}
catch (ParseException ex) {
}
c.setTime(dateFrom);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
dateFrom.getTime();
Date setDate = new Date();
SetDay(c);
Date changeDate = new Date();
for(int i=0;i<=52;i++) {
setDate.setTime(changeDate.getTime());
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 10);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day + 7);
// c.set(Calendar.DAY_OF_WEEK,);
// c.setTime(setDate);
notification = c.getTime();
notificationTime = df.format(notification);
changeDate.setTime(notification.getTime());
Log.i("changedate",String.valueOf(changeDate));
Log.i("dates",notificationTime);
}
}
Thank you..
Upvotes: 0
Views: 330
Reputation: 317562
Please prefer inexact repeating to help the user save battery. This is the reason Android doesn't want you to set exact repeating alarms -- it could drain the battery very quickly.
If you must have exact repeating, you will have to set an exact alarm, then when that fires, register another new exact alarm at the next time you need. Repeat this for as long as you need.
Upvotes: 1