2014-02-18 78 views
0

我想要做一些非常簡單的事情!我有一個sessionTimeout變量,其值爲3600000毫秒,這意味着60分鐘/ 1小時。我需要在60分鐘後設置鬧鐘。我使用下面的代碼,但calendar.getTimeInMillis給了我一個非常高的價值。如果我只是在警報管理器中傳遞3600000,它仍然不起作用,並立即觸發警報接收器。Android AlarmManager觸發一小時後

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

看看這裏接受的答案:http://stackoverflow.com/questions/3447594/android-alarm-manager-with-broadcast-receiver-registered-in-code-rather-than-人。您需要在清單中註冊AlarmReceiver以捕獲警報事件。 –

+1

在旁註中,它看起來像是將sessionTimeout添加到當前時間兩次。 timeInMilliSeconds應該如下設置: long timeInMilliSeconds = calendar.getTimeInMillis(); –

回答

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