我之前已经问过这个,天知道我已经尝试了所有建议。我想在每天给定的时间每天执行一次服务。我能够设置闹钟和广播。但由于某些原因,当警报被解雇时,比如说13点30分,它会在几个不规则的时间间隔内被发射。唯一正常工作的是首先在正确的时间点火,然后它会在一整天都开火,这让我发疯。请帮忙。
PendingIntent reviewsPendingIntent = PendingIntent.getBroadcast(this,0,new Intent(this,ReviewReceiver.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE));
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Random random = new Random();
int low = 7;
int high = 22;
int hour = random.nextInt(high - low) + low;
int minute = random.nextInt(60 - 10) + 10;
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 13);
cal.set(Calendar.MINUTE,15);
cal.set(Calendar.SECOND,5);
cal.set(Calendar.MILLISECOND,0);
if(cal.getTimeInMillis() >= System.currentTimeMillis() ) {
Log.i(TAG,"Set today");
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, reviewsPendingIntent);
}else{
Log.i(TAG,"Set tomorrow");
cal.set(Calendar.DATE,1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, reviewsPendingIntent);
}
N.B。当我得到这个时,我计划使用随机整数设置我的闹钟 工作正常。