AlarmManager setExact与WakefulBroadcastReceiver有时不准确

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

使用Android的19 +

setExact的一起选择与WakefulBroadcastReceiver有时不能按时火(可以是几秒钟或这么晚了)。我的意思是大部分做的一次。大概49次中有50的正确。

我不知道,如果它只是因为系统繁忙的时候,它不能处理的工作量还是什么

下面是我如何设置报警:

AlarmManager alarmMgr =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);   意向意图=新的意图(AlarmReceiver.INTENT_FILTER);   PendingIntent alarmIntent = PendingIntent.getBroadcast(背景下,MyApplication.ALARM_REQUEST_ code,意向,PendingIntent.FLAG_UPDATE_CURRENT);   alarmMgr.setExact(AlarmManager.RTC_WAKEUP,timeToWakeUp,alarmIntent);

下面是我的接收器code:

公共类AlarmReceiver扩展WakefulBroadcastReceiver { 公共静态最后弦乐INTENT_FILTER =myfilter的; @覆盖 公共无效的onReceive(上下文的背景下,意图意图){     意向书的服务=新的意图(背景下,MyWakefulService.class);     startWakefulService(背景下,服务); } }

而在WakefulService

公共类MyWakefulService扩展IntentService { .... @覆盖 保护无效onHandleIntent(意向意图){ ....

解决方案

这行为是API 19又说:

  

使用API​​ 19(奇巧)警报传递开始是不准确的:操作系统将为了尽量减少唤醒和电池使用Shift报警。有新的API,这就需要严格的交付担保支持应用程序;看 setWindow(INT,很长很长,PendingIntent)和 setExact(的int,long,PendingIntent)。应用其targetSdkVersion早于19 API将继续看到在其中,当要求所有报警都提供完全previous行为。

从 AlarmManager 。

重要: setExact()还没有准确的说,作为文档状态:

  

的报警器将被尽可能接近递送到所请求的触发时间。

Using Android 19+

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);

Here is my receiver code:

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); } }

And in the WakefulService

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

解决方案

This behaviour is added in 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.

from AlarmManager.

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

发布评论

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

>www.elefans.com

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