为什么我的 Android 闹钟管理器会立即触发?

发布于 2024-09-11 20:56:54 字数 807 浏览 2 评论 0原文

我正在按照示例代码每 10 秒发送一次更新通知。代码如下,它位于 AppWidgetProviderUpdateService 中。如果我放置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尾戒 2024-09-18 20:56:54

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 at SystemClock.elapsedRealtime(), which is now.

想你只要分分秒秒 2024-09-18 20:56:54

你告诉 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).

撧情箌佬 2024-09-18 20:56:54

如果您正在为过去的时间创建警报的 PendingIntent ,它将立即被触发。示例 - 将闹钟安排在今天上午 8 点,但在上午 11 点左右执行代码将立即触发。

解决方案:

cal.add(Calendar.DATE, 1);

long delay = 24 * 60 * 60 * 1000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), delay,pendingIntent);` 

这将在第二天的指定时间(即上午 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:

cal.add(Calendar.DATE, 1);

long delay = 24 * 60 * 60 * 1000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), delay,pendingIntent);` 

This will fire the event on next day at specified time (i.e 8AM);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文