我已设置闹钟以在60秒后触发。但是警报在80/90秒后被触发(但不是在60秒之后)。如何设置闹钟以在指定时间准确触发?
//calling method to set alarm after 60 seconds
startAlarm(context, 1, System.currentTimeMillis()+60000);
//method defination
public static void startAlarm(Context context, int timerType, long nextScheduledTime) {
EABLog.d(TAG, " ~~~INSIDE START ALARM~~~~~ "+timerType);
Intent intent = new Intent(context, AlarmReceiver.class);
intent.putExtra(Constants.KEY_TIMER_TYPE, timerType);
intent.setAction(""+timerType);
PendingIntent pollPendingIntent = PendingIntent.getBroadcast(context,
timerType, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextScheduledTime,
pollPendingIntent);
EABLog.d(TAG, System.currentTimeMillis()+" ALARM IS SET AT "+nextScheduledTime+" TYPE :"+timerType);
}
//permission added in Android Manifest
<uses-permission android:name="android.permission.WAKE_LOCK"/>
答案 0 :(得分:0)
正如文档所说 setExact - 警报会尽可能传递到所请求的触发时间。所以它并不像你想象的那么精确:)
查看 public void setWindow(int type,long windowStartMillis,long windowLengthMillis,PendingIntent operation) methods第3个参数 - windowLengthMillis 请求的传递窗口的长度,以毫秒为单位。在windowStartMillis之后的这几毫秒内,警报将在不迟于时发送。 这可能会成功:)