0

我非常困惑为什么当我第一次设置警报时它会响起,然后我尝试再次设置它却没有。但是第三次​​它起作用了,第 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++;
4

1 回答 1

1

根据文档,“警报集”中的待处理意图应该是广播接收器(参见此处)。我推荐以下内容:

代替

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()。

于 2013-04-30T15:38:22.143 回答