Android 重复闹钟不起作用

发布于 2024-10-12 18:07:11 字数 898 浏览 4 评论 0原文

这工作正常:

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);

这不起作用。我只听到一次闹钟。

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

我也尝试过这个,没有运气:

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

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

是什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛溺水了 2024-10-19 18:07:11

来自 FLAG_ONE_SHOT 的 PendingIntent 文档

这个PendingIntent可以
只能使用一次。如果设置的话,之后
send() 被调用,它将是
为您和任何人自动取消
未来尝试通过它发送将
失败。

因此,在第一次触发pendingIntent之后,它将被取消,并且下次尝试通过警报管理器发送它将会失败

尝试使用FLAG_UPDATE_CURRENT

From the PendingIntent doc for FLAG_ONE_SHOT:

this PendingIntent can
only be used once. 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.

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

Try using FLAG_UPDATE_CURRENT

北渚 2024-10-19 18:07:11

按顺序查看代码示例:

在第一个示例中,您使用的是 AlarmManager.set - 这严格用于一次性警报,所以是的,它只会触发一次。如果您想使用 AlarmManager.set,那么触发的代码应该做的最后一件事是设置一个新的警报(也应该使用一个新的 PendingIntent)。

在第二个示例中,您使用的是重复警报。您不需要每次触发时都需要创建一个新的 PendingIntent,因为操作系统会处理警报的重复部分。

您的警报没有理由不应该每 3 秒重复一次,因此我将开始查看您编写的用于处理警报的 BroadcastReceiver 实现。

检查您是否已正确实施。注释掉 onReceive() 方法中的所有代码,而只是让它写入日志消息。每次警报触发时,当您看到日志消息出现在 logcat 中时,请重新添加代码(保留日志消息),并将另一条日志消息添加到方法的末尾。这使您可以查看该方法执行需要多长时间 - 您希望它在警报再次触发之前完成,以避免任何意外的副作用。

顺便说一句,如果您想要重复警报,android.os.Handler 是一种更有效的方法,尽管通过 AlarmManager 设置的警报确实非常准确。

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. 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).

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.

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.

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. 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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文