I'm extremely confused as to why when I first set an alarm it goes off, then i try to set it again and it doesn't. BUT the THIRD time it works, and so does the 5th,7th,9th etc... so every 2 times i set the alarm it works. I'm thoroughly confused because I gave my pending intent a uniqID specifically so this doesn't happen but maybe somewhere else I messed up..?
setAlarm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
myCal.set(Calendar.YEAR, datePicker.getYear());
myCal.set(Calendar.MONTH, datePicker.getMonth());
myCal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
myCal.set(Calendar.HOUR, timePicker.getCurrentHour());
myCal.set(Calendar.MINUTE, timePicker.getCurrentMinute());
myCal.set(Calendar.SECOND, 0);
//precaution if the user restarts their phone. Saves the alarm to a file
//and when user restarts their phone register a broadcast receiver to pick up
//the broadcast and then re-create the alarms that were stored.
setAlarms(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth()
, timePicker.getCurrentHour(), timePicker.getCurrentMinute(), assignmentName);
Intent triggered = new Intent(ManageAlarms.this, alarms.DisplayNotification.class);
triggered.putExtra("NotificationId", 1);
triggered.putExtra("assignmentName", assignmentName);
PendingIntent displayIntent = PendingIntent.getActivity(
ManageAlarms.this, counter, triggered, PendingIntent.FLAG_ONE_SHOT);
/*boolean alarmActive = (PendingIntent.getActivity(getBaseContext(), 0,
triggered, PendingIntent.FLAG_NO_CREATE) != null);
if (alarmActive) {
alarmManager.cancel(displayIntent);
}*/
alarmManager.set(AlarmManager.RTC_WAKEUP,
myCal.getTimeInMillis(), displayIntent);
counter++;
According to the documentation, the Pending Intent in "Alarm Set" should be a Broadcast Receiver(See here). I recommend the following:
Replace
PendingIntent displayIntent = PendingIntent.getActivity(
ManageAlarms.this, counter, triggered, PendingIntent.FLAG_ONE_SHOT);
with
PendingIntent displayIntent = PendingIntent.getBroadcast(
ManageAlarms.this, counter, triggered, PendingIntent.FLAG_ONE_SHOT);
The DisplayNotification class should extend BroadcastReceiver and implement onReceive().