【发布时间】:2014-02-25 16:05:33
【问题描述】:
我在我的 MainActivity.java 中注册一个处于 onResume() 状态的警报管理器(这是程序启动的主要活动)
protected void onResume() {
super.onResume();
if (Helper.isNetworkAvailable(this)) {
Intent intent = new Intent(this, NewsIntentService.class);
PendingIntent pi = PendingIntent.getService(this, 0, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
10 * 60 * 1000, pi);
} else {
// nothing done
}
}
但是我得到的结果不一致,以下代码运行良好且没有错误,它表明应该每 10 分钟触发一次 PendingIntent,但结果在以下来自 logcat 的示例中:
它开始运作良好:
2:00 pm (fired), 2:10 pm (fired), 2:30 pm (fired), ...
但过了一段时间:
3:20 pm (fired), 3:27 pm (fired), 3:33 pm (fired), 3:38 pm (fired) ...
question is 在活动的哪个生命周期最好注册一个AlarmManager,如果我所做的是正确的,那么运行不一致的原因是什么。
【问题讨论】:
标签: java android android-alarms