首页 > 解决方案 > NotificationManager 的内存泄漏

问题描述

我从 Activity onCreate 执行此代码。它创建一条通知消息。我的问题是:

当活动被破坏时,我是否需要取消注册通知?我的意思是我们在那里传递一个上下文,据我所知,当活动被破坏时通知会保留,它可能会导致内存泄漏。在 NotificatuonManagerCompat 类上下文中保持可变。

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
mBuilder.setContentTitle("Picture Download")
    .setContentText("Download in progress")
    .setSmallIcon(R.drawable.ic_notification)
    .setPriority(NotificationCompat.PRIORITY_LOW);


int PROGRESS_MAX = 100;
int PROGRESS_CURRENT = 0;
mBuilder.setProgress(PROGRESS_MAX, PROGRESS_CURRENT, false);
notificationManager.notify(notificationId, mBuilder.build())

标签: androidmemory-leaks

解决方案


推荐阅读