我正在开发一个小部件应用程序,在其中我必须每分钟执行一些任务。因此,我正在使用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() + intervalAlso check out the android documentation and this answer for more explanation in Alarms.
更多推荐
Android警报管理器在5秒后重复,并且忽略了间隔时间
发布评论