0

I am trying to do something really simple! I have a sessionTimeout variable which have the value 3600000 milliseconds which means 60 minutes/1 hour. I need to set the alarm after 60 minutes. I am using the following code but calendar.getTimeInMillis gives me a very high value. If I just pass 3600000 in the alarmManager it still does not work and trigger the alarm receiver instantly.

private void setupSessionTimeoutAlarm()
{
    Calendar calendar = Calendar.getInstance();
    calendar.add(calendar.MILLISECOND,(int) sessionTimeout);
    long timeInMilliSeconds = calendar.getTimeInMillis() + sessionTimeout;

    // schedule the alarm
    Intent myIntent = new Intent(Gateway.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(Gateway.this, 0, myIntent,0);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP,timeInMilliSeconds,pendingIntent);
}
Olimpiu POP
  • 5,001
  • 4
  • 34
  • 49
john doe
  • 9,220
  • 23
  • 91
  • 167
  • Look at the accepted answer here: http://stackoverflow.com/questions/3447594/android-alarm-manager-with-broadcast-receiver-registered-in-code-rather-than-man. You need to register the AlarmReceiver in the manifest for it to capture the alarm events. – Josh Feldman Feb 18 '14 at 20:07
  • 1
    On a side note, It looks like you are adding the sessionTimeout to the current time twice. timeInMilliSeconds should be set like this: long timeInMilliSeconds = calendar.getTimeInMillis(); – Josh Feldman Feb 18 '14 at 20:08

1 Answers1

0
  Calendar calendar = Calendar.getInstance();
    Date date = new Date();
    date.setTime(System.currentTimeMillis() + (60 * 60 * 1000));
    calendar.setTime(date);
    Log.i(TAG, "start: " + calendar.getTime().getMinutes());
    Log.i(TAG, "start: " + calendar.getTime().getHours());
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, A3LocationUpdateReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, Configuration.LocationUpdateEveryHoursRequest, intent, 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR,
            pendingIntent);
tej shah
  • 2,995
  • 2
  • 25
  • 35