简体   繁体   English

在Android中多次设置闹钟

[英]Setting Alarm for Multiple times in Android

In my app I want alarms for several times which will be set by the user. 在我的应用程序中,我希望警报由用户设置多次。 But if I set the alarm for a different date and time it only responds on the last set-up time. 但是,如果我将闹钟设置为其他日期和时间,它只会在最后的设置时间响应。

Such as if I set alarm for date 20-01-2017 time 10.10 am , 20-01-2017 time 10.20 am , 20-01-2017 time 10.30 am , and so on it only gives the alarm for last selected date & Time. 例如,如果我为日期20-01-2017时间10.10 am20-01-2017时间10.20 am20-01-2017时间10.30 am设置了警报,依此类推,它仅给出最后一次选择的日期和时间的警报。 But doesn't give alarm at 20-01-2017 time 10.10 am , 20-01-2017 time 10.20 am , or other selected times. 但不会在20-01-2017时间10.10 am20-01-2017时间10.20 am或其他选定时间发出警报。

How can I get alarm at all selected times? 如何在所有选定的时间得到警报? Here is my code: 这是我的代码:

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;
    }
}

Problem is in your void setAlarmN() method. 问题出在您的void setAlarmN()方法中。 You can't generate alarm notification with the same id. 您无法生成具有相同ID的警报通知。 That should mean that notification before gets cancel and override. 那应该意味着通知被取消和覆盖。 Try to set unique id for RQS_1 each time PendingIntent.getBroadcast() gets called. 每次调用PendingIntent.getBroadcast()时,尝试为RQS_1设置唯一的ID。 Probably you have to declare private variable and increment it. 可能您必须声明私有变量并对其进行递增。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM