Reputation: 8302
I have an app that starts a service that continuously updates the notification bar. The notification shows the state of the service. The service is written to sleep for a period and then do some work in the background then repeat. As such, my notifications are strings like:
Idle (3)
Idle (2)
Idle (1)
Working
Idle (3)
...
This all works well and good, the issue I'm seeing is that after a long period of time running, the CPU usage increase for an android process, specifically:
com.android.systemui
I am using "top" through the adb console. When I first start the app, it shows CPU% of 0~5% for com.android.systemui. After 30 minutes, the CPU% is ~80%.
In my class, I have the following code: private NotificationManager _notificationManager; private Notification _notification; private CharSequence _lastNotification;
@Override
public void onCreate() {
super.onCreate();
_notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
_notification = createNotification();
}
@Override
public void onDestroy() {
super.onDestroy();
_notificationManager.cancel(getNotificationId());
}
private Notification createNotification() {
final Notification notification = new Notification();
notification.icon = R.drawable.notification_icon;
notification.tickerText = getText(R.string.serviceStarted);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
final RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
contentView.setImageViewResource(R.id.imageViewIcon, R.drawable.notification_icon);
contentView.setTextViewText(R.id.textViewAppName, getText(R.string.appName));
notification.contentView = contentView;
return notification;
}
private void updateNotification(CharSequence message) {
if (!message.equals(_lastNotification)) {
_notification.contentView.setTextViewText(R.id.textViewState, message);
_notificationManager.notify(getNotificationId(), _notification);
_lastNotification = message;
}
}
I'm at a loss as to why this could be happening. To combat this, I've put a workaround in that will clear the notification after the work is done and then put it back and that seems to have "fixed" the issue. The only problem, of course, is that the notification disappears and reappears every time the work is done.
Upvotes: 2
Views: 1386
Reputation: 6251
take a look at
Huge memory usage when updating the same notification to show progress
and
performance: csipsimple triggering periodic CPU spikes while registered
Upvotes: 2