AlarmManager 不可靠,是否有改进或替代方案?
The AlarmManager is unreliable, are there improvements or alternatives?
我需要一个使用设定值频繁更新的小部件,例如每 1-5 秒更新一次。
我发现为此应该使用 AlarmManager。
我测试了 AlarmManager 的 setExact() 方法,发现它的最短时间恰好是 5 秒。
我还注意到间隔可能会有很大的延迟。
我以 5 秒的设定间隔对此进行了测试。
我的测试显示 setExact() 方法具有以下值:5s、5s、40s、41s、19s、5s、5s、...
使用 setRepeating() 方法显示更差的值:17s、39s、7s、75s、60s,...显然,在三星设备上使用时,AlarmManager 也根本不会触发,这是我计划使用的设备小部件上。
除了使用 AlarmManager 之外,还有其他选择吗?
AlarmManager 能否以某种方式变得更可靠?
我需要更新率是恒定的,没有像这样的延迟。
final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent notifyIntent = new Intent(this, AlarmReceiver.class);
final PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long triggerTime = SystemClock.elapsedRealtime()
+ 3000;
long repeatInterval = 1000;
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
triggerTime, notifyPendingIntent);
除了 AlarmManager,我还检查了 JobScheduler 和 Worker,但两者似乎都以 15 分钟(!)的最小间隔为基础。
然而,使用简单的 Thread 似乎可行。我只需要找出如何可靠地 运行 然后...
如果您知道其他解决方案,请告诉我!
我需要一个使用设定值频繁更新的小部件,例如每 1-5 秒更新一次。 我发现为此应该使用 AlarmManager。 我测试了 AlarmManager 的 setExact() 方法,发现它的最短时间恰好是 5 秒。 我还注意到间隔可能会有很大的延迟。 我以 5 秒的设定间隔对此进行了测试。 我的测试显示 setExact() 方法具有以下值:5s、5s、40s、41s、19s、5s、5s、... 使用 setRepeating() 方法显示更差的值:17s、39s、7s、75s、60s,...显然,在三星设备上使用时,AlarmManager 也根本不会触发,这是我计划使用的设备小部件上。 除了使用 AlarmManager 之外,还有其他选择吗? AlarmManager 能否以某种方式变得更可靠? 我需要更新率是恒定的,没有像这样的延迟。
final AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent notifyIntent = new Intent(this, AlarmReceiver.class);
final PendingIntent notifyPendingIntent = PendingIntent.getBroadcast
(this, NOTIFICATION_ID, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long triggerTime = SystemClock.elapsedRealtime()
+ 3000;
long repeatInterval = 1000;
alarmManager.setExact(AlarmManager.RTC_WAKEUP,
triggerTime, notifyPendingIntent);
除了 AlarmManager,我还检查了 JobScheduler 和 Worker,但两者似乎都以 15 分钟(!)的最小间隔为基础。 然而,使用简单的 Thread 似乎可行。我只需要找出如何可靠地 运行 然后... 如果您知道其他解决方案,请告诉我!