【问题标题】:Repeat alarm everyday accurately (Alarm manager)每天准确重复警报(警报管理器)
【发布时间】:2015-01-17 15:44:24
【问题描述】:

我设置了一个每天重复的闹钟。 但它会有几秒钟或几分钟的错误。 我怎样才能使它更准确?

PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), notificationId, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
long startUpTime = calendar.getTimeInMillis();
if (System.currentTimeMillis() > startUpTime) {
    startUpTime = startUpTime + 24*60*60*1000;
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime,  24*60*60*1000 , pendingIntent);

【问题讨论】:

  • myIntent 是否指向 WakefulBroadcastReceiver

标签: android alarmmanager android-alarms


【解决方案1】:

尝试添加

calendar.set(Calendar.SECOND,00);

变化

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime,  24*60*60*1000 , pendingIntent);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);

【讨论】:

  • 来自 AlarmManager.java: ...从 API 19 开始,所有重复的警报都是不准确的。如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,并按照上述方式重新安排每次时间。
【解决方案2】:

1) 从timepicker基础上获取时间

calNow = Calendar.getInstance();
calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

if (calSet.compareTo(calNow) <= 0) {
    //Today Set time passed, count to tomorrow
    calSet.add(Calendar.DATE, 1);
}

2) 每天设置闹钟

Intent intent = new Intent(AddAlarmNewActivity.this, OnAlarmReceive.class);
intent.putExtra("alarmTitle", mTitle.getText().toString());
intent.putExtra("alarmId", insertedId + "");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 
                                                         (int)insertedId,
                                                         intent,
                                                         PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                          targetCal.getTimeInMillis(), 
                          24*60*60*1000, 
                          pendingIntent);

【讨论】:

    【解决方案3】:
    Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);     
    AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 00);
    calendar.set(Calendar.SECOND, 00);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 1970-01-01
      • 2021-12-23
      相关资源
      最近更新 更多