我对Android中的警报管理器有疑问。 我有以下代码片段来设置应该每周(一次)触发的警报。
// Add the time and set when the notification will be triggered
Calendar setCalendar = item.getDate();
calendar.set(Calendar.MINUTE,setCalendar.get(Calendar.MINUTE)+10080);
//Create a new alarm intent
Intent alarmIntent = new Intent(ApplicationUtils.getApplicationContext(), AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(ApplicationUtils.getApplicationContext(), requestCode, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.ELAPSED_REALTIME, sender);
我必须遵循问题。当周更改时,通知即将发出,并且它们永远不会停止。 有没有人知道如何设置日历以便每周触发一次警报?
谢谢,Arkde
答案 0 :(得分:1)
setRepeating
中的第三个参数不正确。它应该是重复警报之间的间隔(以毫秒为单位)。
一周将是:1000 * 60 * 60 * 24 * 7.
http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.PendingIntent)
答案 1 :(得分:1)
查看它可以帮助您的代码。
Intent intent_for_every_second = new Intent(Activity.this, Notifier.class);
pendingIntent_for_every_second = PendingIntent.getBroadcast(Activity.this, 0, intent_for_every_second,0);
AlarmManager alarmManager_for_every_second = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager_for_every_second.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 1000, 1000,pendingIntent_for_every_second);