Skip to content
Snippets Groups Projects
Commit 7181177c authored by Lennart Goedhart's avatar Lennart Goedhart Committed by GitHub
Browse files

Merge pull request #83 from volkerrichert/master

minor improvements to alarm receiver
parents 7e3e65e0 301cb634
No related branches found
No related tags found
No related merge requests found
......@@ -29,36 +29,43 @@ public class MedtronicCnlAlarmReceiver extends WakefulBroadcastReceiver {
}
public void setContext(Context context) {
if (am != null && pi != null) {
// here is an old alarm. So we cancel it before
cancelAlarm();
}
cancelAlarm();
am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MedtronicCnlAlarmReceiver.class);
pi = PendingIntent.getBroadcast(context, ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
// Setting the alarm to call onRecieve every _REFRESH_INTERVAL seconds
// Setting the alarm in 15 seconds from now
public void setAlarm() {
setAlarm(System.currentTimeMillis());
}
// Setting the alarm to call onRecieve
public void setAlarm(long millis) {
try {
am.cancel(pi);
} catch (Exception ignored) {}
Log.d(TAG, "AlarmManager set to fire at " + new Date(millis));
if (am == null || pi == null)
return;
cancelAlarm();
// don't trigger the past and at least 30 sec away
if (millis < System.currentTimeMillis())
millis = System.currentTimeMillis();
Log.d(TAG, "AlarmManager set to fire at " + new Date(millis));
am.setExact(AlarmManager.RTC_WAKEUP, millis, pi);
}
// restarting the alarm after MedtronicCnlIntentService.POLL_PERIOD_MS from now
public void restartAlarm() {
setAlarm(System.currentTimeMillis() + MedtronicCnlIntentService.POLL_PERIOD_MS);
setAlarm(System.currentTimeMillis() + MedtronicCnlIntentService.POLL_PERIOD_MS + MedtronicCnlIntentService.POLL_GRACE_PERIOD_MS);
}
// Cancel the alarm.
public void cancelAlarm() {
if (am == null || pi == null)
return;
am.cancel(pi);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment