I want to do a function that allows when I click on a button I choose a date and after I receive notification in these dates This code just allows me to receive the last notification and not the others.
String dtStart = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);
Calendar calendar;
Intent intent1;
calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
intent1 = new Intent(getActivity(), AlarmReceiver.class);
intent1.putExtra("titrefr",titlefr);
intent1.putExtra("contentfr",contentfr);
intent1.putExtra("contentar",content);
intent1.putExtra("titrear",title);
intent1.putExtra("audio",stepAudio);
intent1.putExtra("image",image);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1000, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
// am.set(AlarmManager.ELAPSED_REALTIME,calendar.getTimeInMillis(),pendingIntent);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
This is happening just because alarm id is overrides means when you set the alarm and you give 1000 id to each alarm.
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1000, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
So when you set last time.Alarm manager set last time to id 1000 so last time alarm is working. I am simple adding a method that will help you to add multiple alarm
private String setAlarm(int year, int month, int day, int hour, int min) {
String value = "";
calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 00);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
Toast.makeText(AddDrugFormActivity.this, "Select Valid Time", Toast.LENGTH_LONG).show();
value = "INValid";
} else {
value = "Valid";
Intent intentAlarm = new Intent(AddDrugFormActivity.this, AlarmReciever.class);
SharedPreferences preferences= PreferenceManager.getDefaultSharedPreferences(context)
int id = preferences.getInt("id", 0);
PendingIntent pIntent = PendingIntent.getBroadcast(AddDrugFormActivity.this, id, intentAlarm, 0);// set id 0 if single alarm require
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
id = id + 1;
editor = preferences.edit();
editor.putInt("id", id);
editor.commit();
}
return value;
}
You just use this method to add multiple alarm. This is working code.
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.