android-(AudioManager)getSystemService(Context.AUDIO_SERVICE)导致内存泄漏
作者:互联网
我有由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; }
@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;
}
编辑:
堆转储的屏幕截图
解决方法:
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,memory-leaks,android 来源: https://codeday.me/bug/20191030/1965667.html