我正在使用一些警报功能每五秒钟产生一次通知。在函数中有变量,每次调用时都应该改变。但是什么也没发生,它只是继续在通知中显示第一组数据。这是来自 MainActivity 类:
public void setRepeatingAlarm(){
Intent intent = new Intent(this, TimeAlarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (5 * 1000), pendingIntent);
}
这是来自 TimeAlarm 类:
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Homework";
CharSequence message = "test"+ MainActivity.arraytest[x2]+ x2;
x2 +=1;
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,TimeAlarm.class), 0);
Notification notif = new Notification(R.drawable.ic_launcher,"Update", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(notify_id, notif);
}
问题是 x2 变量没有更新。它仅在第一次调用时更新。谢谢。