到目前为止,每天11点钟,警报管理器会触发一个类似的程序:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long alarmTime = calendar.getTimeInMillis();
alarmTime = alarmTime + (AlarmManager.INTERVAL_DAY);
Intent intent = new Intent(context, SyncReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1300, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, AlarmManager.INTERVAL_DAY, pendingIntent);
现在我必须每天随机改变这个程序,我怎样才能以最少的变化做到这一点?
答案 0 :(得分:2)
将setRepeating()
替换为set()
。在SyncReceiver
,选择下一个随机小时,set()
新警报,以便在当时触发SyncReceiver
。