【问题标题】:Alarmmanager.setRepeating triggers ImmediatelyAlarmmanager.setRepeating 立即触发
【发布时间】:2012-10-16 18:52:05
【问题描述】:

这是我的代码:

cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, hourOfDay, minute,Calendar.SECOND);

Intent intent=new Intent(FaceActivity.this,AlarmReceiver.class);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(FaceActivity.this, 0, intent, 0);
        AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

这里的 hourOfDay 是当前小时后的一小时。分钟可能具有相同的值。

现在这里 cal.getTimeInMillis() 设置为比当前时间晚一小时的时间。 但是当我运行这段代码时,广播接收器被立即调用。 谁能告诉我,我做错了什么? 提前致谢...

【问题讨论】:

  • 向我们展示您在cal 中设置时间的代码。我猜cal.getTimeInMillis() 是过去的时间。
  • 代码提供的信息不多。 :) 记录 System.currentTimeMillis();cal.getTimeInMillis()。然后比较结果。
  • @MMohsinNaeem 设置后的时间(573):日历时间为 -62130303706613 当前时间为 1350281798450 日历时间为当前时间过去 2 分钟
  • 所以...? API 说If the time occurs in the past, the alarm will be triggered immediately...........。那么问题出在哪里?
  • @MMohsinNaeem 实际上,如果当前时间是上午 11:55 并且我将闹钟设置为上午 11:58,它应该只在上午 11:58 触发。但在这里它触发了两次,一次在上午 11:55,然后在上午 11:58

标签: android alarmmanager


【解决方案1】:

终于找到了我的错误,我得到了错误的值 YearMonthDaySecond

通过使用下面的代码,它工作得非常好。

cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hourOfDay, minute,cal.get(Calendar.SECOND));

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多