简体   繁体   English

AlarmManager在正确的时间无法在Samsung Phone上唤醒

[英]AlarmManager does not wakeup at right time on Samsung Phone

On some Samsung phones we unfortunately had to find out that the AlarmManager (SamsungAlarmManager) does not work like expected. 不幸的是,在某些三星手机上,我们不得不发现AlarmManager(SamsungAlarmManager)无法正常工作。

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! 我们怀疑,该应用程序将在一段时间(例如1分钟)后被唤醒,但有时计时器醒来时间太晚了!

The logcat output is: logcat的输出为:

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 计时器应在1540473976(Unix)上触发=> 2018-10-25T15:26:16

But why is SamsungAlarmManager set to 15:30:59? 但是,为什么将SamsungAlarmManager设置为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) (看来问题仅出现在装有Android 8.0的Samsung S8和Samsung S7设备上)

After hours of testing we found a workaround to start timers less than 5 min on Samsung android phone. 经过数小时的测试,我们发现了一种解决方法,可以在不到5分钟的三星android手机上启动计时器。

I seems that after some time in idle mode the SamsungAlarmManager does not allow setting timers less than 5 min. 我似乎在空闲模式下经过一段时间后,SamsungAlarmManager不允许将计时器设置为少于5分钟。 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. 如果手机处于空闲模式(打light模式),则由于电池优化,SamsungAlarmManager会将setExact-Time从13s覆盖到5分钟。 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(). 我们的解决方法现在是使用AlarmManager.setAlarmClock()而不是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! 为了避免这种情况,我们在电话处于交互状态时使用setExactAndAllowWhileIdle(),而只有在电话不是交互式时才使用setAlarmClock()!

So, our code to set a timer less than 5 min is now: 因此,我们将计时器设置为少于5分钟的代码现在是:

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! 注意:华为手机与三星手机具有相同的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM