How to know the remaining time for the alarm to ring

Asked

Viewed 524 times

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 the PendingIntent registered on it. You will probably have to use some persistent way to store the alarms that have been created.

  • 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

  • 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.

  • I’m going to take a look at this library and see if I can do anything. Anyway, thank you very much

2 answers

1

Good, in the simplest case of all, you can save in a preferences file the time set for alarm awakening.

So, you take this time, simply subtract it from the current time to find out the remaining time.

One tip I give you is to work with the library joda-time

There are others, but everything but the native Java Date, because on Android there are some problems that you should still find (if not already found).

In joda-time, just do this:

Hours.hoursBetween(LocalDateTime.now(), new LocalDateTime("hora do alarme no formato YYYY-MM-dd HH:mm:ss"));

0

A way to give you a function that returns the hour, minute and second is as follows:

long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * (60 * 1000)) % 60;

String toLeave;
if (diffHours > 0) {
    toLeave = String.format("%dh %dm %ds", diffHours, diffMinutes, diffSeconds);
} else if (diffMinutes > 0) {
    toLeave = String.format("%dm %ds", diffMinutes, diffSeconds);
} else if (diffSeconds > 0) {
    toLeave = String.format("%ds", diffSeconds);
} else {
    return;
}

Where diff is the difference, in milliseconds, between the current time and the time you set the alarm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.