警报管理器不会在android中的确切时间触发警报

编程入门 行业动态 更新时间:2024-10-22 21:23:28
本文介绍了警报管理器不会在android中的确切时间触发警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用日历类安排闹钟如下

I scheduled alarm using Calendar class as below

Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY,1); cal.getTimeInMillis(); cal.set(Calendar.MINUTE,05); long TriggerMillis = cal.getTimeInMillis(); AlarmManager aManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); aManager.set(AlarmManager.RTC_WAKEUP, TriggerMillis,pIntent);

其中 pIntent 是待处理的意图,以便在警报触发时继续进行.

where pIntent is an pending intent to proceed further when alarm triggers.

事件触发延迟几秒钟.使用 Calendar 类来完成这项任务有什么问题吗?有什么建议吗?

The event triggers with few seconds delay. Is that any problem using Calendar class for this task. Any suggestions?

TIA..

推荐答案

你有两个问题:

  • 直接将_WAKEUP 警报与服务一起使用是不可靠的.唯一可靠的模式涉及 WakefulBroadcastReceiver、我的 WakefulIntentService 或类似的东西,其中 PendingIntent 将指向 BroadcastReceiver.

  • It is not reliable to use a _WAKEUP alarm with a service directly. The only reliable patterns involve WakefulBroadcastReceiver, my WakefulIntentService, or something along those lines, where the PendingIntent will be to a BroadcastReceiver.

    如果您的 android:targetSdkVersion 为 19 或更高,并且您在 API 级别 19+ 的设备上运行,则 set() 不准确.理想情况下,您允许它不精确,或者可能使用 setWindow() 来控制它的关闭程度,以最大程度地减少警报事件的功率影响.如果它绝对必须在某个精确的时刻发生,您将需要使用 setExact().由于 setWindow() 和 setExact() 是 API 级别 19 的新功能,因此您需要在旧设备上回退到 set(),通过检查 Build.VERSION.SDK_INT 并相应地进行分支.

    If your android:targetSdkVersion is 19 or higher, and you are running on an API Level 19+ device, set() is inexact. Ideally, you allow it to be inexact, or perhaps use setWindow() to control how off it will be, to minimize the power hit of your alarm event. If it absolutely has to occur at a precise moment, you will need to use setExact(). Since setWindow() and setExact() are new to API Level 19, you will need to fall back to set() on older devices, by examining Build.VERSION.SDK_INT and branching accordingly.

  • 更多推荐

    警报管理器不会在android中的确切时间触发警报

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

    发布评论

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

    >www.elefans.com

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