待定意图仅适用于1个警报

时间:2015-07-17 08:23:59

标签: android android-intent alarm android-pendingintent

目前,我有一个使用pendingIntennt工作的警报,当按下按钮(保存警报)时,该工作正常。问题是,当我安排并保存多个警报时,将关闭的警报只是最新警报。如何才能使所有警报响起?

这是按钮内的代码:

public void saveReminder(View v) {

    final int reminderHour = mReminderTimePicker.getCurrentHour().intValue();
    final int reminderMinute = mReminderTimePicker.getCurrentMinute().intValue();
    final String reminderName = mReminderName.getText().toString();
    final boolean reminderStatus = mReminderSwitchSwitch.isChecked();

    //AlarmManagerHelper.cancelAlarms(this);
    //Updating Reminder Information
    ParseQuery<ParseObject> query = ParseQuery.getQuery("MyReminders");
    query.getInBackground(objectId, new GetCallback<ParseObject>() {
        public void done(ParseObject reminderObject, ParseException e) {
            if (e == null) {
                reminderObject.put("reminderName", reminderName);
                reminderObject.put("reminderHour", reminderHour);
                reminderObject.put("reminderMinute", reminderMinute);
                reminderObject.put("reminderStatus", reminderStatus);
                reminderObject.saveInBackground();
            }
        }
    });
    //set time into calendar instance
    GregorianCalendar calendar= new GregorianCalendar();
    calendar.set(GregorianCalendar.HOUR_OF_DAY,reminderHour);
    calendar.set(GregorianCalendar.MINUTE,reminderMinute);
    calendar.set(GregorianCalendar.SECOND,00);
    calendar.set(GregorianCalendar.MILLISECOND,0000);

    AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);


    Intent reminderintent = new Intent(this, ReminderService.class);
    //push information to reminder screen
    reminderintent.putExtra("reminderName", reminderName);
    reminderintent.putExtra("reminderHour", reminderHour);
    reminderintent.putExtra("reminderMinute", reminderMinute);


    PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 1, reminderintent, PendingIntent.FLAG_UPDATE_CURRENT);
    reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation);


    finish();
}

1 个答案:

答案 0 :(得分:1)

您的所有闹钟看起来都使用相同的requestCode为1,因此每次设置闹钟时,当您拨打PendingIntent.getActivity()时,它会覆盖前一个闹钟。

您需要的是为每个警报设置唯一ID的方法。

使用objectId字符串,您可以通过将字符串中的所有字符相加来生成唯一ID:

    int id = 0;
    for (int i = 0; i < objectId.length(); i++){
        id += objectId.charAt(i);
    }

    PendingIntent operation = PendingIntent.getActivity(getBaseContext(), id, reminderintent, PendingIntent.FLAG_UPDATE_CURRENT);
    reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), operation);