我正在尝试使用警报管理器传递字符串并在BroadcastReceiver中接收它。 但我的问题是我得到了字符串的先前值。
int min = 65;
int max = 80;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(AlarmService.this, AlarmAlertBroadcastReciever.class);
Toast.makeText(this, ""+i1, Toast.LENGTH_LONG).show();
alarmIntent.putExtra("value", String.valueOf(i1));
PendingIntent pendingIntent = PendingIntent.getBroadcast(AlarmService.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 9);
calendar.set(Calendar.SECOND, 0);
//check whether the time is earlier than current time. If so, set it to tomorrow. Otherwise, all alarms for earlier time will fire
if(calendar.compareTo(now) <= 0){
calendar.add(Calendar.DATE, 1);
}
PendingIntent mEverydayPendingIntent = pendingIntent;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mEverydayPendingIntent);
i1生成随机值。 例如,i1生成20然后putExtar传递null。 再次当服务开始时,它产生例如30的随机值 然后putExtra传递先前生成的20个。