是否有适用于Android的准确的重复警报系统? [英] Is there an accurate repeating-alarm system for Android?

查看:79
本文介绍了是否有适用于Android的准确的重复警报系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我目前正在为Android开发一个应用程序,该应用程序围绕着在用户指定的时间发出警报。我的意图是将其用于早上(和第二天早晨-也称为重复警报)叫醒人们。闹钟响起时,它会调用 Activity 活动,该活动具有多个选项(例如贪睡)。我已经完成所有这些工作,但是遇到了问题。




问题



我正在使用 AlarmManager 处理我的警报需求。但是,这堂课有些奇怪(我认为)。有两种建议的方式来处理警报时间的设置。即 setInexactRepeating setRepeating 。这些函数的问题是:

Background


I'm currently developing an application for Android which revolves around an alarm that goes of on an user specified time. My intent for it is that it will be used for waking people up in the morning (and the following morning - aka repeating alarm). When the alarm goes of it will call an Activity that has a couple of options (such as snooze). I've got all of this working but I'm running in to a problem.


Problem


I'm using AlarmManager to handle my alarm needs. There is however something curious going on with the class (in my opinion). There are two adviced ways to handle the setting of the alarm time. Namely setInexactRepeating and setRepeating. The problem with these functions are:


  • setInexactRepeating 不太准确。我的测试表明,该文件在指定的时间被大致激活了,文档显示这一切都含糊不清;

  • setInexactRepeating is not very accurate. My tests have shown that this gets approximately activated at the specified time, which the documentation indicates, all be it rather vaguely;


在此时间之前不会触发警报,但是在第一次调用警报之前可能会延迟几乎整个警报间隔。

the alarm will not fire before this time, but there may be a delay of almost an entire alarm interval before the first invocation of the alarm.

我的测试表明,通常会有5分钟的延迟。在此答案上,用户平均有大约12分钟的延迟。当然,对于应该在特定时间唤醒人们的系统而言,这是行不通的。


My tests show there is usually something of a 5 minute delay. On this answer the user has an average delay of approximately 12 minutes. This won't do, of course, for a system that is supposed to wake people up at their specified times.

setRepeating 确实在指定时间触发。但是,文档指定从API 19开始,所有重复警报都是不精确的。

setRepeating does trigger at the specified time. The docs specify however that as of API 19 that all repeating alarms are inexact. Which is exactly what I don't want.


从API 19开始,所有重复的警报都是不精确的。由于此方法自API 3开始就可用,因此您的应用程序可以安全地调用它,并确保它在当前版本和较旧版本的Android上都将表现出类似的行为。

As of API 19, all repeating alarms are inexact. Because this method has been available since API 3, your application can safely call it and be assured that it will get similar behavior on both current and older versions of Android.


有一个 setExact 方法,但这有点太具体了。除此之外,它没有让我选择一定的时间间隔(每天重复一次警报)。 编辑:在尝试使用 setExact 之后,我发现这将需要我升级到API 19(当前为15)。我想避免。

There is a setExact method, but this is a bit too specific. Aside from that it does not give me the option to have a certain interval (for repeating the alarm daily). After trying to go with setExact I found that this would require me to move up to API 19 (currently on 15), which is something I would like to avoid.


问题



我在该系统上使用了错误的类吗?对我来说,这似乎应该是一种合法的用法,但是通读文档让我感到纳闷。

Question


Am I using the wrong class for this system? To me it seems like it should be a legit usage, but reading through the docs has left me wondering. Is there perhaps another class which is better suited for this system?

推荐答案

您可以在API 19之前和API 19之后进行分离。首次设置警报:

You can separate before API 19 and after API 19. While setting alarm for the first time:

            if (Build.VERSION.SDK_INT >= 19) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), mondayIntent);
            } else {
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY * 7, mondayIntent);
            }

当您收到警报时:

    if (Build.VERSION.SDK_INT >= 19) {
        rescheduleAlarm();
    }

您必须使用


setexact

setexact

再次在rescheduleAlarm中。

again in rescheduleAlarm.

希望这会有所帮助。

这篇关于是否有适用于Android的准确的重复警报系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆