AlarmManager setExact 与 WakefulBroadcastReceiver 有时不准确

编程入门 行业动态 更新时间:2024-10-21 20:43:15
本文介绍了AlarmManager setExact 与 WakefulBroadcastReceiver 有时不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 Android 19+

Using Android 19+

setExact 与 WakefulBroadcastReceiver 结合使用有时不会按时触发(可能会延迟几秒左右).我的意思是大部分时间都是这样.大概 50 次中有 49 次是正确的.

setExact in conjuction with WakefulBroadcastReceiver sometimes does not fire on time (can be a few seconds or so late). I mean most it of the time it does. probably 49 times out of 50 its correct.

我不确定是不是因为当时系统很忙,无法处理工作量还是什么

I'm not sure if its just because the system is busy at the time and it can't handle the workload or what

这是我设置闹钟的方法:

Here is how I set the alarm:

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(AlarmReceiver.INTENT_FILTER); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, MyApplication.ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmMgr.setExact(AlarmManager.RTC_WAKEUP, timeToWakeUp, alarmIntent);

这是我的接收器代码:

public class AlarmReceiver extends WakefulBroadcastReceiver { public static final String INTENT_FILTER = "myfilter"; @Override public void onReceive(Context context, Intent intent) { Intent service = new Intent(context, MyWakefulService.class); startWakefulService(context, service); } }

在 WakefulService 中

And in the WakefulService

public class MyWakefulService extends IntentService { .... @Override protected void onHandleIntent(Intent intent) { ....

推荐答案

API 19 中添加了此行为:

This behaviour is added in API 19:

从 API 19 (KITKAT) 开始传递警报是不准确的:操作系统将转移警报以最大程度地减少唤醒和电池使用.有新的 API 来支持需要严格交付保证的应用程序;见 setWindow(int, long, long, PendingIntent) 和 setExact(int, long, PendingIntent).targetSdkVersion 早于 API 19 的应用程序将继续看到以前的行为,即在请求时准确地传递所有警报.

Beginning with API 19 (KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. There are new APIs to support applications which need strict delivery guarantees; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent). Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested.

来自 AlarmManager.

重要:setExact() 仍然不必精确,正如文档所述:

Important: setExact() still does not have to be exact, as the docs state:

警报将尽可能接近请求的触发时间.

The alarm will be delivered as nearly as possible to the requested trigger time.

更多推荐

AlarmManager setExact 与 WakefulBroadcastReceiver 有时不准确

本文发布于:2023-11-27 04:23:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不准确   AlarmManager   setExact   WakefulBroadcastReceiver

发布评论

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

>www.elefans.com

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