在通知服务的Andr​​oid内存泄漏 [英] android memory leak in notification service

查看:185
本文介绍了在通知服务的Andr​​oid内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它创建了一个通知的服务,然后用特定的信息定期更新它。大约12分钟左右的手机死机和重启后,我相信这是由以下code做的怎么样,我更新通知的内存泄漏,可能会有人请检查/告诉我,如果是这样的话什么我做错了。

I have a service which creates a notification and then updates it with certain information periodically. After about 12 mins or so the phone crashes and reboots, I believe it is caused by a memory leak in the following code to do with how I am updating the notification, could someone please check/advise me if this is the case and what I am doing wrong.

的onCreate:

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

createNotification:

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;
}

updateNotification:

updateNotification:

private void updateNotification(String text){
  contentView.setTextViewText(R.id.text, text);
  mNotificationManager.notify(0, notification);
}

在此先感谢。

Thanks in advance.

推荐答案

我偶然发现了同样的问题。看起来,如果你不高速缓存的远程视窗和通知中的服务,但从头开始的更新程序这个问题就不存在了重新创建。是的,我知道这是效率不高,但至少在手机不会从内存不足的错误重启。

I stumbled upon the same problem. Looks like that if you don't "cache" the RemoteView and Notification in the service, but re-create them from scratch in the "update" routine this problem disappears. Yes, I know it is not efficient, but at least the phone does not reboot from out of memory errors.

这篇关于在通知服务的Andr​​oid内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆