Android警报管理器在5秒后重复,并且忽略了间隔时间 [英] Android Alarm manager is repeating after 5 seconds and ignoring interval time

查看:164
本文介绍了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秒后重复,并且忽略了间隔时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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