问题描述
使用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又说:
从 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:
from AlarmManager.
Important: setExact()
still does not have to be exact, as the docs state:
这篇关于AlarmManager setExact与WakefulBroadcastReceiver有时不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!