2011-08-29 37 views
1

我正在尝试使用此设置来设置每天发生的报警。AlarmManager未设置,每次活动都打开报警

String alarm = Context.ALARM_SERVICE; 
       Calendar calendar = Calendar.getInstance(); 
       AlarmManager am = (AlarmManager)getActivity().getSystemService(alarm); 

        Intent intent = new Intent("NEW_ITEM"); 
        PendingIntent sender = PendingIntent.getBroadcast(getActivity(), 0, intent, 0); 
        calendar.setTimeInMillis(System.currentTimeMillis()); 
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1*AlarmManager.INTERVAL_DAY, sender); 

回答

1

不运行它,代码看起来对我好......很显然,如果你每次启动活动时间设置该报警,报警会立即关闭,因为:am.setRepeating(AlarmManager.RTC_WAKEUP, **calendar.getTimeInMillis()**, 1*AlarmManager.INTERVAL_DAY, sender);告知报警管理,以提醒权现在(第二参数),并在一天重复(第三参数,假设你的常数是正确的)。

如果您希望告警开始只在24小时内,只需将线更改为:

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY, sender); 
+0

好吧,这很酷。我只是想确保警报正在设置,所以如果应用程序在一天内打开,警报仍会出现。 – yoshi24