3

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.

Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
jacosta
  • 349
  • 2
  • 5
  • 17
  • Could you use a fixed view but instead call a setter on it? I haven't dealt with the big content view before, but it seems odd that it's accessed via a public field! – stkent May 18 '15 at 01:03
  • I'm not sure what you mean by fixed view. Where would you be updating the view of the notifcation? – jacosta May 18 '15 at 01:07
  • Ah, fair point, I forgot you essentially replace the notification each time. Are you doing any layout inflation in your update loop? – stkent May 18 '15 at 01:09
  • No just doing what i posted, it seems very odd – jacosta May 18 '15 at 01:22

2 Answers2

1

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

jacosta
  • 349
  • 2
  • 5
  • 17
1

Maybe is a bit late, but I want to tell that it really helps me. My application worked perfectly with in-built notifications. I use an intentService which update my GUI implementing an observer (similar to stopwatch). But when I try to customize the notification with some action buttons (pause and resume) my device initially works well but then it gets hoter and hoter till it's unbearably slow and laggy...

However if I create new RemoteViews every time I update notification, it works like a charm. I don't also know why...Maybe something related with faster access to local variable reference instead global ones??

Just some tip: I have extended RemoteView in order to set more easily click action button. This links explains former:

https://stackoverflow.com/a/22585875/6641443

I hope It helps. Thanks

Community
  • 1
  • 1