I’d like to have a notification done weekly from the day it was set. It initializes when it gets called but not the second time.(I fast forwarded the phone clock to see if it would call it but it didn’t). It must be the 7*calendar.getTimeInMillis(). How else would I go about having it set for weekly?
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, mHour);
calendar.set(Calendar.MINUTE, mMinute);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, OnBootReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
//am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7*calendar.getTimeInMillis(), pendingIntent);
BroadCastReceiver class:
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "from";
CharSequence message = "message";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notif = new Notification(icon, tickerText, when);
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
The
7*calendar.getTimeInMillis()is indeed the problem ascalendar.getTimeInMillis()returns the time since 1970, so you basically set the repeating to ~42.5 * 7 years from now. You need to set the offset, e.g. 7 (days) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (millies).After we cleared that – I suggest you avoid using the repeating, and instead set a new alarm each time the invoked code finishes its work, as there are some possible problems with the repeating mechanism.