android - 为什么我的 android 闹钟管理器会立即启动?

标签 android alarmmanager android-appwidget android-pendingintent

我按照示例代码每 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);

最佳答案

AlarmManager.setRepeating 定义为 public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation) 第二个参数是应该在什么时候第一次调用。您告诉它从 SystemClock.elapsedRealtime() 开始,也就是现在。

关于android - 为什么我的 android 闹钟管理器会立即启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3358984/

相关文章:

java - 关于设置闹钟的非常奇怪的问题

java - 应用小工具 : Ripple effect lost on list item when background is added to outer layout

android - 如何在主屏幕上自动测试小部件?

android - 在应用小部件更新时替换 RemoteViewsFactory

android - Android Studio IDE选择依赖项对话框

javascript - ionic 运行 android 不工作

android - 从 url 下载的图像未显示在 View 中

android - FirebaseInitProvider : FirebaseApp initialization unsuccessful

android - 使用 AlarmManager.RTC 类型调度的 PendingIntent 在 sleep 模式下仍然被调用

java - 我想设置一个通知,当我收到通知时它应该响铃