Android如何设置AlarmManager闹钟每天随机播放?

时间:2015-03-11 11:07:31

标签: android

到目前为止,每天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);

现在我必须每天随机改变这个程序,我怎样才能以最少的变化做到这一点?

1 个答案:

答案 0 :(得分:2)

setRepeating()替换为set()。在SyncReceiver,选择下一个随机小时,set()新警报,以便在当时触发SyncReceiver