AlarmManager setExact与WakefulBroadcastReceiver有时不准确 [英] AlarmManager setExact with WakefulBroadcastReceiver sometimes not exact

查看:1567
本文介绍了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,这就需要严格的交付担保支持应用程序;看<一href="http://developer.android.com/reference/android/app/AlarmManager.html#setWindow(int,%20long,%20long,%20android.app.PendingIntent)"相对=nofollow> setWindow(INT,很长很长,PendingIntent)和<一href="http://developer.android.com/reference/android/app/AlarmManager.html#setExact(int,%20long,%20android.app.PendingIntent)"相对=nofollow> 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有时不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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