PhoneStateListener内存泄漏-android [英] PhoneStateListener memory leak - android

查看:235
本文介绍了PhoneStateListener内存泄漏-android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序运行多次后,内存出现问题.

I am having issue with memory after running many times my app.

java.lang.OutOfMemoryError:位图大小超出VM预算

我发现我以某种方式泄漏了内存,所以我做了一个DUMP HPROF文件,并使用MAT工具找出了问题所在. 事实证明,在运行5次应用程序并退出后,我发现5个Activity实例和5个PhoneStateListener实例.

I figure I was leaking memory somehow so I did a DUMP HPROF file and used the MAT tool to figure out what was wrong. It turns out that after running like 5 times the app and quiting, I find 5 instances of my Activity and 5 instances of PhoneStateListener.

如果我删除对PhoneStateListener的呼叫,则不再存在该问题,并且仅看到我的Activity的一个实例.

If I remove the call to PhoneStateListener, I don't have that issue anymore and I see just 1 instance of my Activity.

问题是,我该如何解决?

The question is, how do I resolve this?

谢谢

丹尼尔

这是我的代码:

OnCreate方法:

OnCreate method:

telephonyManager.listen(mPhoneListener,
                PhoneStateListener.LISTEN_SERVICE_STATE
                | PhoneStateListener.LISTEN_SIGNAL_STRENGTH
                | PhoneStateListener.LISTEN_CALL_STATE
                | PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
                | PhoneStateListener.LISTEN_DATA_ACTIVITY); 

关于活动"类:

PhoneStateListener mPhoneListener = new PhoneStateListener() {
        public void onCallStateChanged(int state,
                java.lang.String incomingNumber) {
        //do something
    }
}

关于onDestroy方法:

on onDestroy method:

telephonyManager.listen(mPhoneListener, PhoneStateListener.LISTEN_NONE);        
mPhoneListener = null;

推荐答案

onDestroy不能保证被调用.来源: onDestroy文档

onDestroy is not guaranteed to get called. Source: onDestroy Docs

您应该尝试清理onPause,然后onResume重新设置您需要的内容.这将有助于清除一些内存压力和泄漏.

You should try cleaning up on onPause and then onResume re-instate the stuff you need. This will help clean up some memory pressure and leaks.

这篇关于PhoneStateListener内存泄漏-android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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