gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:52 55 4
gpt4 key购买 nike

我遇到了由 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 吗?如果是,我该如何更换它?

如果重要的话,我的 Activity 中有这个非静态类,它不会在外部其他地方使用

class GestureListener extends GestureDetector.SimpleOnGestureListener {
RelativeLayout parentLayout;

public void setLayout(RelativeLayout layout){
parentLayout = layout; }
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public 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;
}

编辑:堆转储的屏幕截图 enter image description here

最佳答案

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) 导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244288/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com