1
最近,在我的计时器应用程序中,第一次运行计时器会导致它在大约两或三秒后发出警报。奇怪的是,它只是在我通过Android Studio安装应用程序后第一次运行计时器。每次之后,它都按预期运行,定时器在预期时间后熄灭。为什么AlarmManager警报首次运行延迟?
下面是相关代码:
if(LOG) Log.v(TAG,"Starting the timer service: "+ TimerUtils.time2humanStr(context, mTime));
Intent intent = new Intent(this, TimerReceiver.class);
mPendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mAlarmMgr.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + mTime, mPendingIntent);
变量mTime
正确每次设置,根据logcat的输出:
Starting the timer service: 5 seconds
它最初的运行之后每次工作正常。它以前工作正常;它可能是Android或Android Studio的问题吗?或者我需要初始化一些东西?我知道,如果不将所有的代码都抛弃在这里,我不能期望得到精确的答案,我只是在寻找提示(猜测?),为什么会发生这种行为。
如果是这样的话,为什么会只发生第一次?我试过使用'setExact()',它不会改变。尽管如此,我意识到我对时间错了。它不是双倍的,它只是延迟了几秒钟。 – yuttadhammo 2014-09-24 18:34:18
我认为这是修改文档以反映行为的一种情况。我注意到OP早在API 19之前就描述了什么。 – 2014-09-24 18:41:13