Android notification bar CPU usage inscreases when sending notifications continuously over a long period

StackOverflow https://stackoverflow.com/questions/7791709

Вопрос

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.

Это было полезно?
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top