I am trying to use a calendar to set an alarm manager to go off every Sunday. When I open the app, the alarm starts immediately and repeats everyday instead of once every Sunday.

CODE:

Calendar calendar = Calendar.getInstance();
         calendar.set(Calendar.DAY_OF_WEEK, 1);
         calendar.set(Calendar.HOUR_OF_DAY, 12);
         calendar.set(Calendar.MINUTE, 00);
         calendar.set(Calendar.SECOND, 00);

         am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pi);
有帮助吗?

解决方案

I think you have a math issue. Change to:

24*60*60*1000*7

Since before, you were repeating every 24h

24*60*60*1000 = 86400000 millis = 24h

24*60*60*1000*7 = 24h * 7 = 1 week

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top