问题描述
使用 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 的应用程序将继续看到以前的行为,即在请求时准确地传递所有警报.
来自 AlarmManager.
重要:setExact()
仍然不必精确,正如文档所述:
Important: setExact()
still does not have to be exact, as the docs state:
警报将尽可能接近请求的触发时间.
这篇关于AlarmManager setExact 与 WakefulBroadcastReceiver 有时不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!