AlarmMgr有时触发了正确的,有时是错误的,为什么?

发布于 2025-01-21 14:32:55 字数 3557 浏览 3 评论 0原文

我设置了这样的AlarmMgr:

alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                1000 * 60 * 5L, alarmIntent)

昨天我收到

public class BroadcastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("Execute time:" + Calendar.getInstance().getTime());
}...}

了正确的日志,然后出错了。看起来像是在同一时间触发警报,我不知道为什么。

2022-04-14 10:44:40.447 8663-8663/com.example.ss i/system.out:...
2022-04-14 10:46:40.441 8663-8663/com.example.ss i/system.out:...
2022-04-14 10:48:40.440 8663-8663/com.example.ss i/system.out:...
2022-04-14 10:50:40.444 8663-8663/com.example.ss i/system.out:...
2022-04-14 10:52:40.446 8663-8663/com.example.ss i/system.out:...

我整夜捕捉到日志,这是日志的一部分。 系统日志在此处显示

2022-04-15 07:33:39.945 13580-13580/com.example.ss i/system.out:执行时间:apri apr 15 07:33:39 GMT+09:00 202222222222222222222222222222222222222222222年。 2022-04-15 07:33:39.951 13580-13580/com.example.ss i/system.out.out:执行时间:星期五APR 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.954 13580-13580/com.example.ss i/system.out.out:执行时间:星期五APR 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.960 13580-13580/com.example.ss i/system.out.out:执行时间:星期五APR 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.963 13580-13580/com.example.ss i/system.out.out:执行时间:星期五APR 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.966 13580-13580/com.example.ss i/system.out.out:执行时间:星期五APR 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.969 13580-13580/com.example.ss i/system.out.out:执行时间:星期五APR 15 07:33:39 GMT+09:00 2022
2022-04-15 07:35:40.066 13580-13580/com.example.ss i/system.out.out:执行时间:星期五APR 15 07:35:40 GMT+09:00 2022
2022-04-15 08:40:40.096 13580-13580/com.example.ss i/system.out:执行时间:星期五APR 15 08:40:40 GMT+09:00 2022
2022-04-15 08:40:40.103 13580-13580/com.example.ss i/system.out:执行时间:星期五APR 15 08:40:40 GMT+09:00 2022
2022-04-15 08:40:40.106 13580-13580/com.example.ss i/system.out:执行时间:星期五APR 15 08:40:40 GMT+09:00 2022


。 /实际时间),官方页面表示,系统会触发一些警报以节省电池,警报统计显示,根本没有So Cloesd警报。有时我有正确的输出。我还以15分钟的间隔测试了方法,输出仍然出错。我认为原因不确切。

警报统计:
1000:Android +6S300ms运行,353个唤醒:
+3S273MS 0 WAKES 40警报,最后-4M9S148MS:
警报:com.android.server.action.network_stats_poll
+2S837MS 0 WAKES 226警报,最后-4M9S148MS:
警报:time_tick
+699ms 347唤醒347警报,最后-1H41M9S348MS:
walarm job.delay
+102ms 0唤醒1警报,最后-4M9S148MS:
警报:GraphicsStatsService
+60ms 2唤醒2个警报,最后-2H14M49S133MM:
walarm :ScheduleConditionProvider.evaluate
+31ms 0唤醒1警报,最后-8H39M9S148MS:
警报:android.intent.action.date_changed
+9ms 1唤醒1警报,最后-13H27M11S167MS:
walarm :JS idleness
+8ms 1唤醒1警报,最后-19H41M4S292MS:
walarm :重试
+6ms 2唤醒2个警报,最后-17H41M9S348MS:
walarm job.deadline
U0A40:com.android.providers.calendar +364ms运行,4个唤醒:
+364ms 4唤醒4警报,最后-1H39M9S148MS:
walarm :com.android.providers.calendar.intent.calendarprovider2
U0A89:com.android.systemui +470ms运行,27个唤醒:
+470ms 27唤醒27警报,最后-14H38M7S109MS:
walarm :com.android.internal.policy.impl.phonewindowmanager.delayed_keyguard
U0A113:com.example.ss +4S532MS运行,0个唤醒:
+4S532MS 0 WAKES 185警报,最后-4M9S148MS:
警报:com.example.ss/.broadcastreciever`

I set the alarmMgr like this:

alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                1000 * 60 * 5L, alarmIntent)

and BroadcastReceiver output

public class BroadcastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    System.out.println("Execute time:" + Calendar.getInstance().getTime());
}...}

I got the correct log yesterday, then It's go wrong. It looked like trigger the alarm many times in the same time and I dont know why.I guess may be the android studio logcatch has some unknown mechanism.

2022-04-14 10:44:40.447 8663-8663/com.example.ss I/System.out: ...
2022-04-14 10:46:40.441 8663-8663/com.example.ss I/System.out: ...
2022-04-14 10:48:40.440 8663-8663/com.example.ss I/System.out: ...
2022-04-14 10:50:40.444 8663-8663/com.example.ss I/System.out: ...
2022-04-14 10:52:40.446 8663-8663/com.example.ss I/System.out: ...

I catch the log whole night, a part of the Log shows below.
the system log shows here ,

2022-04-15 07:33:39.945 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.951 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.954 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.960 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.963 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.966 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:33:39 GMT+09:00 2022
2022-04-15 07:33:39.969 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:33:39 GMT+09:00 2022
2022-04-15 07:35:40.066 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 07:35:40 GMT+09:00 2022
2022-04-15 08:40:40.096 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 08:40:40 GMT+09:00 2022
2022-04-15 08:40:40.103 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 08:40:40 GMT+09:00 2022
2022-04-15 08:40:40.106 13580-13580/com.example.ss I/System.out: Execute time:Fri Apr 15 08:40:40 GMT+09:00 2022

About the methods(inexact/actual time),official page said system would trigger together of some alarm in order to save battery, Alarm stats shows that, there is no so cloesd Alarm at all.I have got the correct output sometimes. I also tested method at interval 15mins, the output still went wrong.I don't think the reason is inexact methods.

Alarm Stats:
1000:android +6s300ms running, 353 wakeups:
+3s273ms 0 wakes 40 alarms, last -4m9s148ms:
alarm:com.android.server.action.NETWORK_STATS_POLL
+2s837ms 0 wakes 226 alarms, last -4m9s148ms:
alarm:TIME_TICK
+699ms 347 wakes 347 alarms, last -1h41m9s348ms:
walarm:job.delay
+102ms 0 wakes 1 alarms, last -4m9s148ms:
alarm:GraphicsStatsService
+60ms 2 wakes 2 alarms, last -2h14m49s133ms:
walarm:ScheduleConditionProvider.EVALUATE
+31ms 0 wakes 1 alarms, last -8h39m9s148ms:
alarm:android.intent.action.DATE_CHANGED
+9ms 1 wakes 1 alarms, last -13h27m11s167ms:
walarm:JS idleness
+8ms 1 wakes 1 alarms, last -19h41m4s292ms:
walarm:RETRY
+6ms 2 wakes 2 alarms, last -17h41m9s348ms:
walarm:job.deadline
u0a40:com.android.providers.calendar +364ms running, 4 wakeups:
+364ms 4 wakes 4 alarms, last -1h39m9s148ms:
walarm:com.android.providers.calendar.intent.CalendarProvider2
u0a89:com.android.systemui +470ms running, 27 wakeups:
+470ms 27 wakes 27 alarms, last -14h38m7s109ms:
walarm:com.android.internal.policy.impl.PhoneWindowManager.DELAYED_KEYGUARD
u0a113:com.example.ss +4s532ms running, 0 wakeups:
+4s532ms 0 wakes 185 alarms, last -4m9s148ms:
alarm:com.example.ss/.BroadcastReciever`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2025-01-28 14:32:55
  • 从API 19开始,所有重复的警报都是不精确的。如果您的应用程序需要精确的交付时间,则必须使用一次性的确切警报,并按照上述每次重新安排。 TargetsDkversion比API 19的旧应用程序将继续发出所有警报,包括重复警报,被视为精确。
  • 因此,如果您想实现精确的重复警报,请使用alarmmanager.setExact()和重新安排
  • 参考 google
  • As of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
  • So if you would like to achieve exact repeating alarm use AlarmManager.setExact() and rescheduling
  • Refer google
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文