不幸的是,在某些三星手机上,我们不得不发现AlarmManager(SamsungAlarmManager)无法正常工作。
On some Samsung phones we unfortunately had to find out that the AlarmManager (SamsungAlarmManager) does not work like expected.
我们的代码是:
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); }我们怀疑一段时间后会唤醒应用程序(例如1分钟),但有时计时器醒来的时间太晚了!
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!
logcat输出为:
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计时器应在1540473976(Unix)上触发=> 2018-10-25T15:26:16
The timer should be triggered on 1540473976 (Unix) => 2018-10-25T15:26:16
但是为什么将SamsungAlarmManager设置为15:30:59?
But why is SamsungAlarmManager set to 15:30:59?
这是怎么了?有任何建议吗?
What is Wrong here? Any suggestions?
(看来问题仅在具有Android 8.0的Samsung S8和Samsung S7设备上出现)
(It seems as the Problem is only on Samsung S8 and Samsung S7 devices with Android 8.0)
推荐答案经过数小时的测试,我们发现了一种解决方法,可以在不到5分钟的三星android手机上启动计时器。
After hours of testing we found a workaround to start timers less than 5 min on Samsung android phone.
在空闲模式下经过一段时间后,SamsungAlarmManager不允许设置少于5分钟的计时器。如果手机处于空闲模式(打light模式),则由于电池优化,SamsungAlarmManager会将setExact-Time从13s覆盖到5分钟。将应用程序放入电池优化白名单(不受电池监控的应用程序)无济于事! (似乎三星简单地忽略了此列表!!!!)
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!!!)
我们的解决方法现在使用的是AlarmManager.setAlarmClock()而不是setExactAndAllowWhileIdle()。但是这种解决方案的缺点是在右上角显示了一个闹钟图标。 为避免这种情况,我们在电话处于交互状态时使用setExactAndAllowWhileIdle(),而只有在电话不交互时才使用setAlarmClock()!
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!
因此,我们的代码设置了计时器现在不到5分钟:
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!
更多推荐
AlarmManager在正确的时间无法在Samsung Phone上唤醒
发布评论