On some Samsung phones we unfortunately had to find out that the AlarmManager (SamsungAlarmManager) does not work like expected.
Our code is:
private void scheduleNextSamplingIntent(final Context context, final int intervalInMillis) {
Log.v(TAG, "scheduleNextSamplingIntent | intervalInMillis = " + intervalInMillis);
if (intervalInMillis <= 0) {
Log.w(TAG, "scheduleNextSamplingIntent | invalid interval " + intervalInMillis);
return;
}
long currentTimeMillis = DeviceClock.getInstance().getCurrentTimeMillis();
long triggerAtMillis = currentTimeMillis + intervalInMillis;
Log.v(TAG, "scheduleNextSamplingIntent | currentTimeMillis = " + currentTimeMillis);
Log.v(TAG, "scheduleNextSamplingIntent | triggerAtMillis = " + triggerAtMillis);
PendingIntent samplingPendingIntent = getSamplePendingIntent(context);
AlarmManagerCompat alarmManager = new AlarmManagerCompat(context);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, samplingPendingIntent);
}
We suspect, that the application will be waked up after a time (for example 1min) , but sometimes the timer is waking up some minutes too late!
The logcat output is:
10-25 15:26:03.118 32734 32734 I MotionDetectorService: Analyzed motion: SLEEPING
10-25 15:26:03.120 32734 32734 D MotionDetector: symptomaticStateGracePeriodOver ? 1540473963119 - 1540473823091 = 140028 ?> 300000
10-25 15:26:03.120 32734 32734 D MotionDetector: Still symptomatic but grace period not over yet. Keep gracing ...
10-25 15:26:03.121 32734 32734 V MotionDetectorService: notifyListeners | MotionDetector
10-25 15:26:03.121 32734 32734 V MotionDetectorService: scheduleNextSamplingIntent | intervalInMillis = 13000
10-25 15:26:03.122 32734 32734 V MotionDetectorService: scheduleNextSamplingIntent | currentTimeMillis = 1540473963121
10-25 15:26:03.122 32734 32734 V MotionDetectorService: scheduleNextSamplingIntent | triggerAtMillis = 1540473976121
10-25 15:26:03.137 3781 4353 D SamsungAlarmManager: setExact Intent (T:0/F:5/AC:false) 20181025T153059 - CU:10227/CP:32734
The timer should be triggered on 1540473976 (Unix) => 2018-10-25T15:26:16
But why is SamsungAlarmManager set to 15:30:59?
What is Wrong here? Any suggestions?
(It seems as the Problem is only on Samsung S8 and Samsung S7 devices with Android 8.0)