我正在使用一些闹钟功能每5秒钟产生一次通知。在函数中,每次调用它时都会有变化。但没有任何事情发生,它只是继续显示通知中的第一组数据。 这是从MainActivity类别:通知中未更新的Android变量
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变量没有更新。它只在第一次调用时才会更新。 谢谢。
我不知道你为什么每隔5秒使用通知,但这是一个糟糕的设计选择。我会亲自卸载每隔5秒发送一次通知的应用程序。 –
它只是用于测试 –
其中是变量x2宣布?我猜这是TimeAlarm的私人领域?如果是这样,它将在每次闹铃中重新初始化。在这种情况下,使其静态。否则发布所有/更多的TimeAlarm类 –