Android 通知栏长时间连续发送通知时CPU占用率增加

发布于 12-10 17:07 字数 1851 浏览 3 评论 0原文

我有一个应用程序启动一项不断更新通知栏的服务。该通知显示服务的状态。该服务被编写为休眠一段时间,然后在后台执行一些工作,然后重复。因此,我的通知是这样的字符串:

Idle (3)

Idle (2)

Idle (1)

working

Idle (3)

...

这一切都运行良好,我看到的问题是经过很长一段时间后运行时,Android 进程的 CPU 使用率增加,特别是:

com.android.systemui

我通过 adb 控制台使用“top”。当我第一次启动应用程序时,它显示 com.android.systemui 的 CPU% 为 0~5%。 30 分钟后,CPU% 约为 80%。

在我的课堂上,我有以下代码: 私有通知管理器_notificationManager; 私人通知_通知; 私有 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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文