我正在使用一些警报功能每五秒生成一次通知。在函数中有一些变量,每次调用时都应该改变。但是什么也没有发生,它只是继续显示通知中的第一组数据。 这是来自 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 变量没有更新。它仅在首次调用时更新。 谢谢。
最佳答案
将 x2 变量设为静态似乎可以解决问题。
关于Android 变量未在通知中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12663990/