简体   繁体   English

Android-有时会使用AlarmManager在错误的时间触发警报

[英]Android - Alarms sometimes fires at the wrong time using AlarmManager

This problem seems a little bit odd, but if someone as encounter something like this, please help me... 这个问题似乎有点奇怪,但是如果有人遇到这样的事情,请帮助我...

I created an Alarm Scheduler, that sends an alarm to the user using AlarmManager , through this code: 我创建了一个警报调度程序,该警报调度程序使用AlarmManager通过以下代码向用户发送警报:

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra("tk_alert_id", lastAlertId.getId()+"");
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, idRandom, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);

The problem is that, sometimes, I receive this alarm on my AlarmReceiver ( BroadcastReceiver ) at a wrong time, as you can see in the image bellow: image , and I can't figure out what's the problem... I checked the time for date and was set as "2015-05-27 17:00:00", but it was received a little minutes earlier (around 16:57) ... 问题是,有时我会在错误的时间在我的AlarmReceiver( BroadcastReceiver )上收到此警报,如您在下面的图像image中看到的: image ,但我不知道是什么问题...我检查了时间日期,并设置为“ 2015-05-27 17:00:00”,但几分钟前(大约16:57)收到了此邮件...

Does anyone knows what kind of problem I am encountering here? 有谁知道我在这里遇到什么样的问题?

For API levels <19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time. 对于<19的API级别,您应该使用AlarmManager.setRepeating(),并且警报将在指定的时间准确触发。

Api levels >=19 and above this no longer works. 大于或等于19的Api级别将不再起作用。 There was change in android so that all repeating alarms are inexact. android中发生了更改,因此所有重复的警报都不精确。

So if you would like to achieve exact repeating alarm use AlarmManager.setExact(). 因此,如果您想实现精确的重复警报,请使用AlarmManager.setExact()。

See this question for more info. 有关更多信息,请参见此问题

Edit For your purpose (a one-off alarm, at a precise time) use alarmManager.setExact(....). 编辑为达到您的目的(在某个特定时间发出一次警报),请使用alarmManager.setExact(....)。 See docs 查看文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM