我正在遵循一个每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);
amAlarm1.setInexactRepeating(AlarmManager.RTC_WAKEUP, 120000, AlarmManager.INTERVAL_DAY, alarm1Pending);
,但是仍然会立即触发挂起意图? - Muhammad BabarsetRepeating
,它像魔术一样正常工作 :) - Muhammad Babar