AlarmManager火灾警报器在错误的时间警报器、火灾、错误、时间

2023-09-11 12:09:34 作者:鬼〆柏

我设法一切创造用于消防的通知作为警报结果的通知服务的所有权利。不幸的是,设置使用AlarmManager报警器不工作的权利。它发射几分钟后(不完全是小时,这将表明一个时区的问题)。反复出现的周期为1周,所以我用了恒INTERVAL_DAY和7倍增为了确保一个PendingIntent不会取代其他的,我通过一个dayOfWeek作为第二个参数PendingIntent.getService()。我检查的时间为正确的报警,消防登录吧:

I managed everything all right to create a notification service used to fire a notification as a result of an alarm. Unfortunately, setting the alarm using AlarmManager doesn't work right. It fires several minutes later (not exactly hours, which would indicate a timezone problem). The recurring period is 1 week, so I used the constant INTERVAL_DAY and multiplied it with 7. In order to make sure that one PendingIntent doesn't replace the other, I pass the dayOfWeek as second parameter to PendingIntent.getService(). I check the correctness of the time for the alarm to fire by logging it:

Log.d(TAG, "next alarm " + df.format(cal.getTime()));

难道真的没有办法列出设置的所有报警 - 至少是那些从我自己的应用程序?我相信这是追查错误的唯一方法。

Is there really no way to list all alarms set - at least those from my own app? I believe this is the only way to track down the error.

我的code:

cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.DATE, 1);
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
Log.d(TAG, "next alarm " + df.format(cal.getTime()));
Intent showNotificationIntent = new Intent(context, NotificationService.class);
dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
alarmIntent = PendingIntent.getService(context, dayOfWeek, showNotificationIntent, 0);
getAlarmManager(context).setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    INTERVAL_WEEK, alarmIntent);

我想提供每天有一个报警,但在不同的时间,这可以由用户来设置。所以我用了7报警,这应激发每周。

I want to offer to have an alarm every day, but at various times, which can be set by the user. So I use up to 7 alarms, which should fire on a weekly basis.

即使阅读了无数的答案类似的问题(我不打算创建一个重复的问题),我没有设法找到了问题。

Even after reading the numerous answers to similar questions (I don't intend to create a duplicate question), I haven't managed to find the problem.

推荐答案

对于低于19 API的水平,你应该使用 AlarmManager.setRepeating()和你的警报会触发正好在指定的时间。你的API级别为19及以上,这将不再工作。有变化的android,使所有重复报警是不精确的。你会因此如果想实现精确的重复报警,你应该安排与 AlarmManager.setExact(),然后报警时,报警触发再做一遍下周等每星期。

For api levels below 19 you should use AlarmManager.setRepeating() and your alarms will trigger exactly at specified time. Thou on api levels 19 and above this will no longer work. There was change in android so that all repeating alarms are inexact. So if you would like to achieve exact repeating alarm you should schedule alarm with AlarmManager.setExact() and then when alarm triggers do it again for next week and so on every week.