我按照示例代码每 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);
最佳答案
AlarmManager.setRepeating 定义为 public void setRepeating (int type, long triggerAtTime, long interval, PendingIntent operation)
第二个参数是应该在什么时候第一次调用。您告诉它从 SystemClock.elapsedRealtime()
开始,也就是现在。
关于android - 为什么我的 android 闹钟管理器会立即启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3358984/