警报管理器不会在 android 中的准确时间触发警报 [英] Alarm manager not triggering alarms at exact time in android

查看:42
本文介绍了警报管理器不会在 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..

推荐答案

你有两个问题:

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

  1. 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 中的准确时间触发警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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