简体   繁体   English

Android重复闹钟无效

[英]Android repeating alarm not working

This works fine: 这很好用:

Intent intent = new Intent(HelloAndroid2.this, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid2.this, 0,
    intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), pendingIntent);

This doesn't work. 这不起作用。 I hear the alarm only time. 我只听到闹钟的声音。

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), 3 * 1000, pendingIntent);

I have also tried this, no luck: 我也试过这个,没有运气:

Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 5);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7000, pendingIntent);

What is the problem? 问题是什么?

From the PendingIntent doc for FLAG_ONE_SHOT: 来自FLAG_ONE_SHOT的PendingIntent文档

this PendingIntent can only be used once. 此PendingIntent只能使用一次。 If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail. 如果设置,在调用send()之后,它将自动为您取消,并且将来通过它发送的任何尝试都将失败。

So after the pendingIntent is fired the first time, it will be cancelled and the next attempt to send it via the alarm manager will fail 因此,在pendingIntent第一次触发后,它将被取消,下一次通过警报管理器发送它的尝试将失败

Try using FLAG_UPDATE_CURRENT 尝试使用FLAG_UPDATE_CURRENT

Looking at your code samples in order: 按顺序查看代码示例:

In your first sample you are using AlarmManager.set - this is strictly for one-off alarms so yes, it will only fire once. 在您的第一个示例中,您正在使用AlarmManager.set - 这仅用于一次性警报,所以是的,它只会触发一次。 If you want to use AlarmManager.set then the last thing the code triggered should do is to set a fresh alarm (which should also use a fresh PendingIntent). 如果你想使用AlarmManager.set,那么代码触发的最后一件事就是设置一个新的警报(它也应该使用一个新的PendingIntent)。

In your second example you are using a repeating alarm. 在您的第二个示例中,您正在使用重复警报。 You do not need to create a fresh PendingIntent each time this fires as the OS takes care of the repeating aspect of the alarm. 每次触发时都不需要创建新的PendingIntent,因为操作系统会处理警报的重复方面。

There is no reason why your alarm should not repeat every 3 seconds, so I would start looking at the BroadcastReceiver implementation you have written to handle the alarm. 没有理由说你的警报不应该每3秒重复一次,所以我会开始查看你编写的BroadcastReceiver实现来处理警报。

Check that you've implemented it properly. 检查您是否已正确实施。 Comment out all the code in the onReceive() method and instead just have it writing a log message. 注释掉onReceive()方法中的所有代码,而只是让它编写一条日志消息。 Once you see your log message appearing in the logcat every time the alarm fires, add your code back in (keeping the log message), and another log message to the end of the method. 每当警报触发时,您都会在logcat中看到日志消息,请将代码重新添加(保留日志消息),并将另一条日志消息添加到方法的末尾。 This allows you to see how long the method takes to execute - you want it to be finished before the alarm fires again to avoid any unexpected side effects. 这允许您查看方法执行所需的时间 - 您希望在警报再次触发之前完成该操作以避免任何意外的副作用。

As an aside, if you want a repeating alarm, android.os.Handler is a much more efficient approach although alarms set through AlarmManager do fire very accurately. 顺便说一句,如果你想要一个重复警报,android.os.Handler是一种更有效的方法,虽然通过AlarmManager设置的警报可以非常准确地触发。

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

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