Android AlarmManager在一小时后触发

时间:2014-02-18 19:46:40

标签: android

我正在尝试做一些非常简单的事情!我有一个sessionTimeout变量,其值为3600000毫秒,这意味着60分钟/ 1小时。我需要在60分钟后设置闹钟。我使用以下代码,但calendar.getTimeInMillis给了我很高的价值。如果我只是在alarmManager中传递3600000它仍然无法工作并立即触发警报接收器。

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);
}

1 个答案:

答案 0 :(得分: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);