写多个警报,警报管理器会覆盖它们吗?

时间:2013-04-30 13:13:22

标签: android alarmmanager

我非常困惑为什么当我第一次设置闹钟时它会熄灭,然后我尝试再次设置它而不是。但它的第三次工作时间,第5次,第7次,第9次等也是如此......所以我每隔2次设置它起作用的警报。我彻底搞糊涂了,因为我特意将我的未决意图给了一个uniqID,所以这不会发生,但也许在其他地方我搞砸了......?

setAlarm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

                myCal.set(Calendar.YEAR, datePicker.getYear());
                myCal.set(Calendar.MONTH, datePicker.getMonth());
                myCal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
                myCal.set(Calendar.HOUR, timePicker.getCurrentHour());
                myCal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
                myCal.set(Calendar.SECOND, 0);

                //precaution if the user restarts their phone. Saves the alarm to a file
                //and when user restarts their phone register a broadcast receiver to pick up
                //the broadcast and then re-create the alarms that were stored. 
                setAlarms(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth()
                        , timePicker.getCurrentHour(), timePicker.getCurrentMinute(), assignmentName);

                Intent triggered = new Intent(ManageAlarms.this, alarms.DisplayNotification.class);
                triggered.putExtra("NotificationId", 1);
                triggered.putExtra("assignmentName", assignmentName);

                PendingIntent displayIntent = PendingIntent.getActivity(
                        ManageAlarms.this, counter, triggered, PendingIntent.FLAG_ONE_SHOT);

                /*boolean alarmActive = (PendingIntent.getActivity(getBaseContext(), 0,
                        triggered, PendingIntent.FLAG_NO_CREATE) != null);

                if (alarmActive) {
                    alarmManager.cancel(displayIntent);
                }*/

                alarmManager.set(AlarmManager.RTC_WAKEUP, 
                        myCal.getTimeInMillis(), displayIntent);
                counter++;

1 个答案:

答案 0 :(得分:1)

根据文档,"报警集"中的待定意图应该是广播接收器(See here)。我推荐以下内容:

替换

PendingIntent displayIntent = PendingIntent.getActivity(
                        ManageAlarms.this, counter, triggered, PendingIntent.FLAG_ONE_SHOT);

PendingIntent displayIntent = PendingIntent.getBroadcast(
                        ManageAlarms.this, counter, triggered, PendingIntent.FLAG_ONE_SHOT);

DisplayNotification类应该扩展BroadcastReceiver并实现onReceive()。