I am working on adding multiple alarm functionality in my existing app. Earlier, the alarm was single only and every thing worked fine. I have been able to set multiple alarms, it is working fine too, but problem is with snooze feature. Earlier, snooze was working perfect. But, after multiple alarm implementation it is not working. I never get dialog for snoozing alarm, once I press snooze button. Here is my code:
Set alarm method
public void setAlarm(boolean AlarmEnabled, int AlarmHour, int AlarmMin) {
//Build Intent/Pending Intent for setting the alarm
Intent AlarmIntent = new Intent(MultipleAlarmActivity.this, AlarmReceiver.class);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
int _id = (int)System.currentTimeMillis();
AlarmIntent.putExtra("REQUEST CODE", _id);
Log.v("MultipleAlarmActivity RequestCode", ""+_id);
PendingIntent Sender = PendingIntent.getBroadcast(MultipleAlarmActivity.this, _id, AlarmIntent, 0);
//Build Calendar objects for setting alarm
Calendar curCalendar = Calendar.getInstance();
Calendar alarmCalendar = Calendar.getInstance();
//Initialize Seconds and Milliseconds to 0 for both calendars
curCalendar.set(Calendar.SECOND, 0);
curCalendar.set(Calendar.MILLISECOND, 0);
alarmCalendar.set(Calendar.SECOND, 0);
alarmCalendar.set(Calendar.MILLISECOND, 0);
//Update alarmCalendar with Alarm Hour and Minute Settings
alarmCalendar.set(Calendar.HOUR_OF_DAY, AlarmHour);
alarmCalendar.set(Calendar.MINUTE, AlarmMin);
//If Alarm Time is now or in the past, set it for tomorrow 24 hours in advance from time selected
if (alarmCalendar.getTimeInMillis() <= curCalendar.getTimeInMillis()) {
alarmCalendar.add(Calendar.HOUR, 24);
}
//Set the alarm
AlmMgr.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), Sender);
//Build the Strings for displaying the alarm time through Toast
String CalendarHourStr;
if (AlarmHour > 12) {
CalendarHourStr = Integer.toString(AlarmHour - 12);
} else {
CalendarHourStr = Integer.toString(AlarmHour);
}
String CalendarMinStr = Integer.toString(AlarmMin);
if (AlarmMin < 10) {
CalendarMinStr = "0" + CalendarMinStr;
}
String strAmPM;
if (AlarmHour < 12) {
strAmPM = "AM";
}
else {
strAmPM = "PM";
}
String alarmTime = Integer.toString(alarmCalendar.get(Calendar.MONTH) + 1) + "/" + Integer.toString(alarmCalendar.get(Calendar.DAY_OF_MONTH)) + "/" + Integer.toString(alarmCalendar.get(Calendar.YEAR)) + " " + CalendarHourStr + ":" + CalendarMinStr + " " + strAmPM;
Toast.makeText(this, "Alarm Set For " + alarmTime, Toast.LENGTH_LONG).show();
Log.v("MultipleAlarmActivity setAlarm", "in if loop");
Log.v("MultipleAlarmActivity setAlarm", "setAlarm called");
}
Alarm Receiver
public class AlarmReceiver extends BroadcastReceiver
{
public static final String ALARM_ALERT_ACTION = "com.android.alarmclock.ALARM_ALERT";
public static final String ALARM_INTENT_EXTRA = "intent.extra.alarm";
@Override
public void onReceive(Context context, Intent intent)
{
Intent in = new Intent(Intent.ACTION_MAIN);
in.setClass(context, SnoozeActivity.class);
Log.v("AlarmReceiver RequestCode", ""+intent.getIntExtra("REQUEST CODE", 0));
in.putExtra("REQUEST CODE", intent.getIntExtra("REQUEST CODE", 0));
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(in);
}
}
Method for snoozing alarm
private void snooze()
{
//Set Calendar Value for Snooze Alarm
Calendar calendar = Calendar.getInstance();
//int snoozeTime = mMinute + SNOOZE_MIN;
calendar.add(Calendar.MINUTE, SNOOZE_MIN); //SNOOZE_MIN = 1;
long snoozeTime = calendar.getTimeInMillis();
//Build Intent and Pending Intent to Set Snooze Alarm
Intent AlarmIntent = new Intent(SnoozeActivity.this, AlarmReceiver.class);
AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE);
AlarmIntent.putExtra("REQUEST CODE", req_code);
PendingIntent Sender = PendingIntent.getBroadcast(SnoozeActivity.this, req_code, AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlmMgr.set(AlarmManager.RTC_WAKEUP, snoozeTime, Sender);
timer.cancel();
}