The following code checks the time point of next alarm and calculates the corresponding time duration until the alarm occurs.
private AlarmInfo CheckForNextAlarm(Context context) {
AlarmInfo alarmInfo = new AlarmInfo("","");
try {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager!=null)
{
AlarmManager.AlarmClockInfo clockInfo = alarmManager.getNextAlarmClock();
if (clockInfo!=null)
{
long nextAlarmTime = clockInfo.getTriggerTime();
Date nextAlarmDate = new Date(nextAlarmTime);
android.text.format.DateFormat df = new android.text.format.DateFormat();
alarmInfo.alarmTime = df.format("EEE HH:mm", nextAlarmDate).toString();
Date dateNow = new Date();
long diffMilliSec = nextAlarmDate.getTime() - dateNow.getTime();
long seconds = diffMilliSec / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
if (hours > 0)
{
alarmInfo.durationUntilAlarm += hours + " h";
minutes -= (hours*60);
}
if ((hours > 0) && (hours <= 99))
{
alarmInfo.durationUntilAlarm += " ";
}
if (hours <= 99)
{
alarmInfo.durationUntilAlarm += minutes + " m";
}
}
}
if (alarmInfo.alarmTime.isEmpty())
{
alarmInfo.alarmTime = "" + Settings.System.getString(context.getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);
}
} catch (Exception e) {
}
return alarmInfo;
}