Java 更新通知时设置ContentView最终会冻结应用程序?(安卓)

Java 更新通知时设置ContentView最终会冻结应用程序?(安卓),java,android,mobile,service,notifications,Java,Android,Mobile,Service,Notifications,我有一个每毫秒更新一次通知的服务(秒表) 它一开始工作正常,问题是,应用程序最终会变慢,秒表更新看起来非常滞后。我已经指出了这个问题,因为我正在重置通知的contentview。如果我注释掉该代码,计时器将无限期地正常运行。如果我把这一行留在里面,计时器和应用程序会在大约1-2分钟后显著减速 创建通知的代码: notificationContent.setImageViewResource(R.id.image, R.drawable.ic_main); notificationContent.

我有一个每毫秒更新一次通知的服务(秒表)

它一开始工作正常,问题是,应用程序最终会变慢,秒表更新看起来非常滞后。我已经指出了这个问题,因为我正在重置通知的contentview。如果我注释掉该代码,计时器将无限期地正常运行。如果我把这一行留在里面,计时器和应用程序会在大约1-2分钟后显著减速

创建通知的代码:

notificationContent.setImageViewResource(R.id.image, R.drawable.ic_main);
notificationContent.setTextViewText(R.id.exerciseName, _currentExercise.getTitle());
notificationContent.setTextViewText(R.id.setNumber, "Set " + _currentSet + "/" + _currentExercise.getSets());
notificationContent.setTextViewText(R.id.timeElapsed, getFormattedElapsedTime());
notificationContent.setOnClickPendingIntent(R.id.notifButton, setComplete);
mBuilder.setSmallIcon(icon); //for some reason I need this for my view to show up

mBuilder.setContentIntent(contentIntent);
notification = mBuilder.build();
notification.bigContentView = notificationContent;

notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;

//attach notification and ensure service continues to run in foreground after activity is destroyed
startForeground(NOTIFICATION_ID, notification);
更新通知的代码(每毫秒调用一次):


那一行:
notification.bigContentView=notificationContent创建减速。如果我删除它,应用程序将无限期地平稳运行。如果我不使用它,我的应用程序会变慢。而且随着时间的推移,速度也会变慢。就像它在一分钟后开始减速,到了5分钟,它的速度慢得让人无法忍受。我不知道为什么更新通知的视图会导致这种情况。任何帮助都将不胜感激。

我似乎通过每次更新通知时创建一个新的RemoteView解决了这个问题

    RemoteViews newNotifContent = new RemoteViews(getPackageName(), R.layout.custom_notification);
    newNotifContent.setTextViewText(R.id.exerciseName, _currentExercise.getTitle());
    newNotifContent.setTextViewText(R.id.setNumber, getString(R.string.sets_prefix, _currentSet, _currentExercise.getSets()));
    newNotifContent.setTextViewText(R.id.timeElapsed, getFormattedElapsedTime());

    Notification notif = mBuilder.build();
    notif.bigContentView = newNotifContent;

    mNotificationManager.notify(NOTIFICATION_ID,notif);

现在没有延迟,但我真的不知道为什么每毫秒创建一个新的remoteviews对象就可以修复它。如果有人知道的话,请随意插话

也许有点晚了,但我想告诉大家,这确实对我有帮助。 我的应用程序与内置通知完美配合。我使用一个intentService来更新实现观察者的GUI(类似于秒表)。 但是,当我尝试用一些动作按钮(暂停和恢复)定制通知时,我的设备最初工作得很好,但随后它变得越来越热,直到速度慢得让人无法忍受

然而,如果我每次更新通知时都创建新的RemoteView,它会像一个符咒一样工作。我也不知道为什么…可能与更快地访问局部变量引用而不是全局变量有关

只是一些提示:我扩展了RemoteView,以便更容易地设置单击操作按钮。此链接解释了前者:


我希望有帮助。谢谢

你能用固定的视角,而不是叫一个设定者吗?我以前没有处理过大内容视图,但它是通过公共字段访问的,这似乎很奇怪!我不知道你说的固定视野是什么意思。你会在哪里更新通知的视图?啊,公平点,我忘了你每次都会替换通知。在你的更新循环中,你有没有进行布局膨胀?不只是做我发布的,这看起来很奇怪
    RemoteViews newNotifContent = new RemoteViews(getPackageName(), R.layout.custom_notification);
    newNotifContent.setTextViewText(R.id.exerciseName, _currentExercise.getTitle());
    newNotifContent.setTextViewText(R.id.setNumber, getString(R.string.sets_prefix, _currentSet, _currentExercise.getSets()));
    newNotifContent.setTextViewText(R.id.timeElapsed, getFormattedElapsedTime());

    Notification notif = mBuilder.build();
    notif.bigContentView = newNotifContent;

    mNotificationManager.notify(NOTIFICATION_ID,notif);