在Android

时间:2017-01-26 12:38:04

标签: java android alarmmanager

在我的应用程序中,我想要多次警报,这将由用户设置。 但是如果我将警报设置为不同的日期和时间,它只会响应最后的设置时间。

例如,如果我为日期20-01-2017时间10.10 am设置闹钟, 20-01-2017时间10.20 am20-01-2017时间10.30 am, 等等它只给出最后一个选定日期和闹钟的警报。时间。 但不会在20-01-2017时间10.10 am20-01-2017时间10.20 am或其他选定时间发出警报。

如何在所有选定时间获得警报? 这是我的代码:

public void setDate(View view) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

public void setTime(View view) {

    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}


public void setAlarm(View view) {
    Calendar calNow = Calendar.getInstance();
    Calendar calSet = (Calendar) calNow.clone();
    calSet.set(Calendar.HOUR_OF_DAY, alarmHour);
    calSet.set(Calendar.MINUTE, alarmMin);
    calSet.set(Calendar.DAY_OF_MONTH, alarmDay);
    calSet.set(Calendar.YEAR, alarmYear);
    calSet.set(Calendar.MONTH, alarmMonth);

    setAlarmN(calSet);
}

private void setAlarmN(Calendar targetCal) {

makeText(this, "Alarm is set at" + targetCal.getTime(),
        LENGTH_LONG).show();
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
        getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
        pendingIntent);


}

//date picker fragment
public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        alarmDay = day;
        alarmYear = year;
        alarmMonth = month;
    }
}

//Time picker fragment
public static class TimePickerFragment extends DialogFragment
        implements TimePickerDialog.OnTimeSetListener {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        alarmHour = hourOfDay;
        alarmMin = minute;
    }
}

1 个答案:

答案 0 :(得分:0)

您的void setAlarmN()方法存在问题。您无法生成具有相同ID的警报通知。这应该意味着之前的通知取消和覆盖。每次调用RQS_1时,请尝试为PendingIntent.getBroadcast()设置唯一ID。可能你必须声明私有变量并递增它。