I'm developing an alarm clock. Below I have a function to set the alarm. But I want to know how to find the remaining time for the AlarmManager to trigger the PendingIntent.
For example, it is now 11:00 AM, and we set AlarmManager to trigger PendingIntent at 11:00 PM, and by the calculations, we know PendingIntent will be called in 12 hours. But how to discover this remaining time?
Thanks in advance for your attention
String schedule = "23:00"; //exemplo
Calendar cal = Calendar.getInstance();
cal.set(cal.HOUR_OF_DAY, getTime(schedule));
cal.set(cal.MINUTE, getMinute(schedule));
cal.set(cal.SECOND, 0);
cal.set(cal.MILLISECOND, 0);
DateFormat dfH = new SimpleDateFormat("HH");
DateFormat dfM = new SimpleDateFormat("mm");
int currentTime = Integer.parseInt(dfH.format(new Date()));
int currentMinute = Integer.parseInt(dfM.format(new Date()));
Intent i = new Intent(context, RecebAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context.getApplicationContext(), id, i, 0);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
long totalTime = cal.getTimeInMillis();
if (currentTime > getTime(schedule) || (currentTime == getTime(schedule) && currentMinute >= getMinute(schedule))) {
alarms.set(AlarmManager.RTC_WAKEUP, totalTime + AlarmManager.INTERVAL_DAY, pi);
} else {
alarms.set(AlarmManager.RTC_WAKEUP, totalTime, pi);
}