AlarmManager does not wakeup at right time on Sams

2019-08-23 07:38发布

问题:

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)

回答1:

After hours of testing we found a workaround to start timers less than 5 min on Samsung android phone.

I seems that after some time in idle mode the SamsungAlarmManager does not allow setting timers less than 5 min. If the phone is in idle mode (doze light mode) the SamsungAlarmManager overwrite the setExact-Time from 13s to 5 min because of Battery optimization. Putting the App into the whitelist of Battery-optimization (Battery unmonitored apps) doesn't help anything! (It seems that Samsung simple ignores this list!!!)

Our workaround is now using AlarmManager.setAlarmClock() instead of setExactAndAllowWhileIdle(). But this solution has the disadvantage of showing an alarm-clock-icon in the upper right corner. To avoid this, we are using setExactAndAllowWhileIdle() when the phone is interactive and setAlarmClock() only if it's not!

So, our code to set a timer less than 5 min is now:

public void setExactAndAllowWhileIdleOrAlarmClockWhenNotInteractive(final int type, final long triggetAtMillis, final PendingIntent operation) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !mPowerManager.isInteractive()) {
        Log.v(TAG, "setExactAndAllowWhileIdleOrAlarmClockWhenNotInteractive | not interacive | setAlarmClock = " + triggetAtMillis);
        mAlarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(triggetAtMillis, operation), operation);
    } else {
        Log.v(TAG, "setExactAndAllowWhileIdleOrAlarmClockWhenNotInteractive | interacive | setExactAndAllowWhileIdle = " + triggetAtMillis);
        setExactAndAllowWhileIdle(type, triggetAtMillis, operation);
    }
}

Note: The Huawei phones have the same Problem's like the Samsung phones!