简体   繁体   中英

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. But doesn't give alarm at 20-01-2017 time 10.10 am , 20-01-2017 time 10.20 am , or other selected times.

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. You can't generate alarm notification with the same 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. Probably you have to declare private variable and increment it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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