ste*_*ter 14 notifications android memory-leaks notificationmanager
我有一个服务,它会创建一个通知,然后定期用某些信息更新它.电话崩溃并重新启动大约12分钟后,我认为这是由于以下代码中的内存泄漏导致我如何更新通知,有人请检查/建议我是否是这种情况和我我做错了.
的onCreate:
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Run Code Online (Sandbox Code Playgroud)
createNotification:
private void createNotification() {
Intent contentIntent = new Intent(this,MainScreen.class);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent appIntent =PendingIntent.getActivity(this,0, contentIntent, 0);
contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.text, "");
notification = new Notification();
notification.when=System.currentTimeMillis();
notification.contentView = contentView;
notification.contentIntent = appIntent;
}
Run Code Online (Sandbox Code Playgroud)
updateNotification:
private void updateNotification(String text){
contentView.setTextViewText(R.id.text, text);
mNotificationManager.notify(0, notification);
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我偶然发现了同样的问题.看起来如果你没有在服务中"缓存"RemoteView和Notification,但是在"更新"例程中从头开始重新创建它们,这个问题就会消失.是的,我知道它效率不高,但至少手机不会因内存不足错误而重启.