报警管理器没有在android中的确切时间触发报警 [英] Alarm manager not triggering alarms at exact time in android
问题描述
我使用Calendar类别安排闹钟
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
或者沿着这些行的东西,其中
It is not reliable to use a
_WAKEUP
alarm with a service directly. The only reliable patterns involveWakefulBroadcastReceiver
, myWakefulIntentService
, or something along those lines, where thePendingIntent
will be to aBroadcastReceiver
.
如果您的 android:targetSdkVersion
是19或更高版本,并且您正在运行在一个API级别19+的设备上, set()
是不精确的。理想情况下,您允许它不精确,或者可以使用 setWindow()
来控制它将如何关闭,以最小化您的报警事件的功率命中。如果它绝对必须在一个确切的时刻发生,您将需要使用 setExact()
。由于 setWindow()
和 setExact()
是API第19级的新功能,您需要回退到<$在旧设备上,通过检查 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屋!