我在 Android 中开发闹钟时遇到问题。
当我触发包含必须在计划时间启动的 Activity
的 PendingIntent
时,它运行良好。
但是,当闹钟响起并且我按贪睡 5 分钟并使用不同的请求代码触发相同的 Activity
时,它会在我按贪睡后立即启动。
这是我在 PushActivity
中的部分源代码:
private void setAlarm() {
final int FIVE_MINUTES = 1000 * 60 * 5;
int replacedCode = alarmCode + 1;
attempt = attempt+1;
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, PushActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(PushActivity.this,
replacedCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, FIVE_MINUTES,
pendingIntent);
}
最佳答案
闹钟类型为 ELAPSED_REALTIME_WAKEUP
的 set()
的时间值需要根据 SystemClock.elapsedRealtime()
指定。如果您希望在五分钟后执行某项操作,请使用 SystemClock.elapsedRealtime()+FIVE_MINUTES
,而不仅仅是 FIVE_MINUTES
。
关于java - PendingIntent 的触发时间比我预期的要早,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21965960/