问题描述
我正在开发一个小部件应用程序,在其中我必须每分钟执行一些任务。因此,我正在使用AlarmManager来实现这一点。但是无论我设置了什么间隔时间,间隔都会每5秒重复一次。
我正在使用如下所示的AlarmManager:
最终AlarmManager警报=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
长间隔= 60000;
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime(),interval,endingIntent);
预先感谢。
AlarmManager.ELAPSED_REALTIME
用于从系统启动以来触发警报。而 AlarmManager.RTC
使用UTC时间。
alarm.setInexactRepeating(AlarmManager .ELAPSED_REALTIME,SystemClock.elapsedRealtime(),间隔,pendingIntent);
这将在系统启动后开始运行,并以指定的间隔重复。
alarm.setInexactRepeating(AlarmManager.RTC,calendar.getTimeInMillis(),interval,endingIntent);
这将从从现在开始运行,并以指定的间隔重复。 / p>
要解决此问题,建议使用 AlarmManager.RTC
。如果您想在1分钟后启动闹钟,然后重复一次,然后像下面这样传递第二个参数:
calendar.getTimeInMillis ()+间隔
也请查看android 和此在警报中获取更多说明。
I am working on a widget application where I have to perform some task in every one minute. So, I am using AlarmManager to achieve this. But no matter whatever I set the interval time, its being repeated in every 5 seconds.
I am using AlarmManager like this:
final AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pendingIntent);
long interval = 60000;
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), interval, pendingIntent);
Thanks in advance.
AlarmManager.ELAPSED_REALTIME
is used to trigger the alarm since system boot time. Whereas AlarmManager.RTC
uses UTC time.
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), interval, pendingIntent);
This will start running after system boots and repeats at the specified interval.
alarm.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), interval, pendingIntent);
This will start running from now and repeats at the specified interval.
To solve the problem, I suggest using AlarmManager.RTC
. In case you want to start the alarm after 1 minute and then repeat, then pass the second param like this:
calendar.getTimeInMillis() + interval
Also check out the android documentation and this answer for more explanation in Alarms.
这篇关于Android警报管理器在5秒后重复,并且忽略了间隔时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!