Android警报管理器在5秒后重复,并且忽略了间隔时间

编程入门 行业动态 更新时间:2024-10-25 04:25:43
本文介绍了Android警报管理器在5秒后重复,并且忽略了间隔时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个小部件应用程序,在其中我必须每分钟执行一些任务。因此,我正在使用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 文档和此 answer 在警报中获取更多说明。

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秒后重复,并且忽略了间隔时间

本文发布于:2023-11-27 06:59:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637060.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报   间隔时间   忽略了   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!