为什么我的 Android 闹钟管理器会立即触发?
我正在按照示例代码每 10 秒发送一次更新通知。代码如下,它位于 AppWidgetProvider
的 UpdateService
中。如果我放置 Thread.sleep(10*1000); 我可以看到服务循环的预期行为。显然,我有一些根本性的错误,会立即触发。它应该是一个 PendingIntent
警报,它将向我的侦听器广播更新。
long nextUpdate = 10*1000;
Log.d(TAG, "Requesting next update in " + nextUpdate + " msec." );
Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
updateIntent.setClass(this, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
// Schedule alarm, and force the device awake for this update
AlarmManager alarmManager = (AlarmManager)getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
nextUpdate, pendingIntent);
I am following sample code for sending an update notification every 10'seconds. The code follows and it is in an UpdateService
for an AppWidgetProvider
. If I put a Thread.sleep(10*1000);
I can see the expected behavior of my servicing loop. I obviously have something fundamentally wrong that is triggering immediately. It is supposed to be a PendingIntent
of an alarm that will broadcast update to my listener.
long nextUpdate = 10*1000;
Log.d(TAG, "Requesting next update in " + nextUpdate + " msec." );
Intent updateIntent = new Intent(ACTION_UPDATE_ALL);
updateIntent.setClass(this, UpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, updateIntent, 0);
// Schedule alarm, and force the device awake for this update
AlarmManager alarmManager = (AlarmManager)getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
nextUpdate, pendingIntent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AlarmManager.setRepeating 定义为
public void setRepeating (int type, long triggerAtTime, longinterval, PendingIntent operation)
第二个参数是第一次调用它的时间。您告诉它从SystemClock.elapsedRealtime()
开始,即现在。AlarmManager.setRepeating is defined as
public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
The 2nd argument is when it should be first called. You're telling it to start atSystemClock.elapsedRealtime()
, which is now.你告诉
setRepeating()
您希望第一个闹钟立即响起 (SystemClock.elapsedRealtime()
)。如果您希望第一个闹钟在其他时间响起,请添加一个偏移量 (SystemClock.elapsedRealtime()+nextUpdate
)。You are telling
setRepeating()
that you want the first alarm to go off immediately (SystemClock.elapsedRealtime()
). If you want the first alarm to go off some other time, add an offset (SystemClock.elapsedRealtime()+nextUpdate
).如果您正在为过去的时间创建警报的 PendingIntent ,它将立即被触发。示例 - 将闹钟安排在今天上午 8 点,但在上午 11 点左右执行代码将立即触发。
解决方案:
这将在第二天的指定时间(即上午 8 点)触发事件;
If you are creating PendingIntent of an alarm for past time it will be fired immediately. Example - Schedule alarm for today 8AM but executing code around 11AM will fire immediately.
Solution:
This will fire the event on next day at specified time (i.e 8AM);