我使用日历类安排闹钟如下
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中的确切时间触发警报
发布评论