[Android] setRepeating() の誤差と setExact() setWindow()

Android 19 からAlarmMangerでの、set()、setRepeating()などの時間経過の誤差が大きくなりました。(inexact になったとあります)
理由は、バッテリー電力消費量を改善するためのようですが、実際どの程度なのか試してみました。




setRepeating(), set()のinexact

 
確かに、毎朝7時のアラームが7時1分であれば、多少は許容される場合もあるでしょうが
そもそも誤差がどれだけなのかよくわかりません。OSに依存させているというのは、けっこう出たとこ勝負ではないでょうか。
 
テストを実施してみました。
Activity から Service を呼び、AlarmManager にそれぞれのメソッドをセットします
アラーム起動は1秒後で、その後1分間隔で繰り返すテストです
Nexus7 2013 を使用
またスマホはwakeupの状態
targetSdkVersion = 25 です
 

setRepeating

 


相当ばらついています
1分間隔ですが24秒後にアラームが出てることもあります
 
1x1.trans - [Android] setRepeating() の誤差と setExact()  setWindow()
 

set

 
setは単発アラームなので、Serviceでアラーム起動後、次のアラームを設定する方法をとりました


 
毎回設定しているためか、setRepeating よりはマシですが
時々ズレます
 
1x1.trans - [Android] setRepeating() の誤差と setExact()  setWindow() 

setExact

 


 
setExact のReferenceでは
http://developer.android.com/reference/android/app/AlarmManager.html#setExact(int, long, android.app.PendingIntent)

This method is like set(int, long, PendingIntent), but does not permit the OS to adjust the delivery time. The alarm will be delivered as nearly as possible to the requested trigger time.

ということで、努力目標でしょうか
CPUに負荷がかかってしまうと確実ではないような感じですね
 
1x1.trans - [Android] setRepeating() の誤差と setExact()  setWindow()
 

setWindow

 
 
window の幅は 1000 msec に設定
 


 
SetWindow のReferenceでは
http://developer.android.com/reference/android/app/AlarmManager.html#setWindow(int, long, long, android.app.PendingIntent)

Schedule an alarm to be delivered within a given window of time. This method is similar to set(int, long, PendingIntent), but allows the application to precisely control the degree to which its delivery might be adjusted by the OS.

windowの範囲内であればOSが調整してくれる(たぶん)
ということですね
 
1x1.trans - [Android] setRepeating() の誤差と setExact()  setWindow()
 
window の範囲内でアラームが出ていますが
繰り返しによる遅れがだんだん積み重なっています
setWindow、setExact 以外に繰り返しのAPIがないようです

x setExactRepeatingというのが無い

setWindowがとりあえず、バッテリー消費と時間精度の妥協策でしょうか

シェアする

  • このエントリーをはてなブックマークに追加

フォローする