jacosta jacosta - 4 months ago 14
Java Question

Setting ContentView when updating a notification eventually freezes up the app? (Android)

I have a service that updates a notification every millisecond (stopwatch).

It works fine initially, the problem is, the app eventually slows down and stopwatch updates look really laggy. I've pinpointed this issue to the fact that I'm resetting the notification's contentview. If i comment out that code, the timer runs fine indefinitely. If I leave that line in, the timer and app slow down significantly after about 1-2 minutes.

code to create notification:

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);


code to update notification (called every millisecond):

notificationContent.setTextViewText(R.id.exerciseName, _currentExercise.getTitle());
notificationContent.setTextViewText(R.id.setNumber, getString(R.string.sets_prefix, _currentSet, _currentExercise.getSets()));
notificationContent.setTextViewText(R.id.timeElapsed, getFormattedElapsedTime());

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

mNotificationManager.notify(NOTIFICATION_ID,notification);


That one line:
notification.bigContentView = notificationContent;
creates the slow down. If I remove that, the app runs smoothly indefinitely. If I leave it in, my app slows down. And it gets slower overtime too. Like it starts to slow down after a minute, and by 5 minutes, its unbearably slow and laggy. I don't know why updating the notification's view would cause this. Any help would be greatly appreciated.

Answer

I seemingly solved it by creating a new remoteviews every time I update the notification.

    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);

There is no lag now but I really don't know why making a new remoteviews object every millisecond fixes it. If anyone knows, feel free to chime in

Comments