AlarmManager在设定时间之前熄灭

时间:2013-05-03 01:42:09

标签: android alarmmanager repeatingalarm

我的AlarmManger出现了问题:

以下正确添加一天(因为时间9:25已经过去)输出正确但AlarmManager在几秒钟内调用广播。这是相关的snippit。有什么想法吗?

Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 21);
    cal.set(Calendar.MINUTE, 25);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);


    Intent intent = new Intent(this, NotificationReceiver.class);
    NotificationReceiver instance = new NotificationReceiver();
    intent.setAction(instance.NOTIFICATION_SEND);
    intent.putExtra("TYPE",instance.NOTIFICATION_TYPE_PROGRESS);
    intent.putExtra("Ticker","Waiting for arrival.");
    intent.putExtra("ID",1);
    intent.putExtra("title","Check-In");
    intent.putExtra("message","A message");

    if(System.currentTimeMillis()>cal.getTimeInMillis()){
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY||cal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
            cal.add(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
            Log.d("ALARMS","Set for monday.");
        }
        else{
            cal.add(Calendar.DATE,1);
            Log.d("ALARMS","Set for tomorrow. "+cal.get(Calendar.DAY_OF_MONTH));
        }
    }
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24332, intent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(),4000,pendingIntent);

1 个答案:

答案 0 :(得分:1)

解决:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 24, intent, PendingIntent.FLAG_UPDATE_CURRENT);