1
I am 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 Alarmmanager to trigger Pendingintent.
For example, it’s now 11:00, and we’ve set up Alarmmanager to trigger Pendingintent at 11:00 p.m., and by calculations, we know that Pendingintent will be called in 12 hours. But how to find that remaining time?
Thanks 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);
}
Searching well, it seems that there is no way to consult the
AlarmManager
thePendingIntent
registered on it. You will probably have to use some persistent way to store the alarms that have been created.– Wakim
I am storing the data using Sqlite. String Schedule = "23:00"; It’s just an example, but alarms are set by the user. I’m trying to create a function to do this calculation, because from what I’ve seen, there’s nothing ready
– Brunnu
Oh yes, you will have to convert to Calendar and do arithmetic. There is the library
Joda Time
(http://www.joda.org/joda-time/), which has methods that assist in this calculation.– Wakim
I’m going to take a look at this library and see if I can do anything. Anyway, thank you very much
– Brunnu