Android 变量未在通知中更新

标签 android mobile

我正在使用一些警报功能每五秒生成一次通知。在函数中有一些变量,每次调用时都应该改变。但是什么也没有发生,它只是继续显示通知中的第一组数据。 这是来自 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/

相关文章:

android - 我在创建聊天应用程序时收到 ConcurrentModificationException

html - 在开发人员工具中测试响应能力时,网站 react 完美,但不是以实际手机屏幕为中心?

android - 如何在以编程方式在android中上传到服务器之前减小视频的大小

java - 如何在 Intellij IDEAS/Android Studio 中制作链接到源代码中特定行的日志?

qt - QML,一次编码,两次部署

jquery - CSS - 停止跨度内的文本溢出

mobile - 从经典 ASP 检测移动用户代理并在 session 开始时重定向

javascript - jqMobi 使用 ajax 数据动态更新元素

android - XAxis 标签在 MPAndroidChart 中绘制不正确

android - 游标在没有事先 close() 警告的情况下完成。这是什么意思?