android-(AudioManager)getSystemService(Context.AUDIO_SERVICE)导致内存泄漏

android-(AudioManager)getSystemService(Context.AUDIO_SERVICE)导致内存泄漏,第1张

概述我有由AudioManager引起的内存泄漏.因此,我在代码中注释了这一行,以查看它是否可以解决我的问题:publicclassFireRoomActivityextendsActivity{AudioManageram;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);am=

我有由AudioManager引起的内存泄漏.因此,我在代码中注释了这一行,以查看它是否可以解决我的问题:

public class FireRoomActivity extends Activity {AudioManager am;protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    am = (AudioManager)getSystemService(Context.AUdio_SERVICE);  }}

它确实解决了问题,而且我没有内存泄漏了.那是因为Context.AUdio_SERVICE吗?如果是,那我该如何更换呢?

如果重要的话,我的活动中有一个此非静态类,该类不在其他地方使用

class GestureListener extends GestureDetector.SimpleOnGestureListener {relativeLayout parentLayout;public voID setLayout(relativeLayout layout){    parentLayout = layout;  }@OverrIDepublic boolean onDown(MotionEvent e) {    return true;}// event when double tap occurs@OverrIDepublic boolean onDoubleTap(MotionEvent e) {         makeArrowsVisible();    parentLayout.findVIEwByID(R.ID.cabinet_zoomed).setVisibility(VIEw.INVISIBLE);    button key = (button)parentLayout.findVIEwByID(R.ID.key);    if(key!=null){        key.setVisibility(VIEw.INVISIBLE);}    return true;}

编辑:
堆转储的屏幕截图

解决方法:

https://gist.github.com/jankovd/891d96f476f7a9ce24e2中提到的修复对我有用.

public class ActivityUsingVIDeoVIEw extends Activity {  @OverrIDe protected voID attachBaseContext(Context base) {    super.attachBaseContext(AudioServiceActivityLeak.preventLeakOf(base));  }}/** * Fixes a leak caused by AudioManager using an Activity context.  * Tracked at https://androID-revIEw.Googlesource.com/#/c/140481/1 and * https://github.com/square/leakcanary/issues/205 */public class AudioServiceActivityLeak extends Contextwrapper {  AudioServiceActivityLeak(Context base) {    super(base);  }  public static Contextwrapper preventLeakOf(Context base) {    return new AudioServiceActivityLeak(base);  }  @OverrIDe public Object getSystemService(String name) {    if (Context.AUdio_SERVICE.equals(name)) {      return getApplicationContext().getSystemService(name);    }    return super.getSystemService(name);  }}

感谢Dejan Jankov 总结

以上是内存溢出为你收集整理的android-(AudioManager)getSystemService(Context.AUDIO_SERVICE)导致内存泄漏全部内容,希望文章能够帮你解决android-(AudioManager)getSystemService(Context.AUDIO_SERVICE)导致内存泄漏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1087353.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存