我也尝试使用ApplicationContext
,但由于某些原因它仍然泄漏。
在AdActivity leak on AdMob (SDK 7.0) for Android上找到了与此问题类似的帖子,但没有答案。
还尝试在onDestroy()中将adlistener和ad设置为null,但是没有任何运气,并且仍然会泄漏活动。
我的代码在onCreate()中调用
private void refreshInterstitial(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("AD_ID");
mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("877BCC97E130A0DC62B2E5770D854496").build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mInterstitialAd.show();
}
@Override
public void onAdClosed() {
}
});
}
泄漏泄漏痕迹
┬───
│ GC Root: Global variable in native code
│
├─ mx instance
│ Leaking: UNKNOWN
│ ↓ mx.a
│ ~
├─ com.google.android.gms.ads.internal.webview.w instance
│ Leaking: UNKNOWN
│ mContext instance of com.google.android.gms.ads.internal.webview.ay, not wrapping activity
│ View#mParent is null
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ w.a
│ ~
├─ com.google.android.gms.ads.internal.webview.aa instance
│ Leaking: YES (View detached and has parent)
│ mContext instance of com.google.android.gms.ads.internal.webview.ay, not wrapping activity
│ View#mParent is set
│ View#mAttachInfo is null (view detached)
│ View.mWindowAttachCount = 1
│ ↓ aa.mListenerInfo
├─ android.view.View$ListenerInfo instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ View$ListenerInfo.mOnClickListener
├─ com.google.android.gms.ads.nonagon.ad.webview.f instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ f.a
├─ com.google.android.gms.ads.nonagon.ad.webview.l instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ l.e
├─ com.google.android.gms.ads.nonagon.ad.event.bs instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ bs.a
├─ java.util.HashMap instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ HashMap.table
├─ java.util.HashMap$Node[] array
│ Leaking: YES (aa↑ is leaking)
│ ↓ HashMap$Node[].[1]
├─ java.util.HashMap$Node instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ HashMap$Node.key
├─ com.google.android.gms.ads.nonagon.shim.k instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ k.a
├─ com.google.android.gms.ads.internal.client.ae instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ ae.a
├─ com.google.android.gms.internal.ads.zzuc instance
│ Leaking: YES (aa↑ is leaking)
│ ↓ zzuc.zzcbw
├─ com.test.Activity$1 instance
│ Leaking: YES (aa↑ is leaking)
│ Anonymous subclass of com.google.android.gms.ads.AdListener
│ ↓ EqualizerActivity$1.this$0
╰→ com.test.Activity instance
Leaking: YES (ObjectWatcher was watching this because Activity received Activity#onDestroy() callback and Activity#mDestroyed is true)
key = 40a1eb8e-c9e6-4062-b5f7-053e642e812f
watchDurationMillis = 5288
retainedDurationMillis = 258
参考方案
根据InterstitialAd Docs:
单个InterstitialAd对象可用于在活动的整个生命周期内请求和显示多个插页广告,因此您只需构造一次即可。
再次查看您的代码后,我注意到您在每次调用mInterstitialAd
方法时都重新构造了refreshInterstitial()
。但是根据上面的文档,您应该在mInterstitialAd
期间仅构造一次onCreate()
。
在您的情况下,是导致内存泄漏的主要原因:您仍然有一个活动的侦听器(绑定到Activity的寿命),但是您使用另一个侦听器重建了一个新的InterstitialAd
实例。
因此,解决方案是在不重新分配的情况下重用InterstitialAd
实例及其侦听器。我建议将您的refreshInterstitial()
方法简化为:
private void refreshInterstitial() {
mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("877BCC97E130A0DC62B2E5770D854496").build());
}
然后将mInterstitialAd
分配放入onCreate()
中。此解决方案类似于可以找到here的解决方案。
我有一个可以通过https调用网络服务的应用程序。当我在手机上运行apk时,效果很好。但是,在模拟器中,所有通过POST的SSL请求均失败,并显示以下信息: 读取错误:ssl = 0xb402be00:SSL库失败,通常是一个协议 错误 错误:100c50bf:SSL例程:ssl3_read_bytes:NO_RENEGOTIATION(外部/无聊的sl /…
绑定Java库Xamarin.Android - java我花了两天时间在每个论坛,文档,tuto,博客等上寻找答案。我为实习生启动了一个Android应用程序,因为我不懂Java,所以用xamarin C#开发了它。直到最近一切都还不错,但现在我需要集成一个SDK才能在应用程序中使用POS(销售点),但是该库是用Java编写的,即使跟随文档或辅导老师,我也无法将其与xamarin绑定(我什至无法调试)。这里有人已…
Android Studio报告“android sdk解析不正确” - java我只是将一个android项目从eclipse迁移到了android studio,但是当我尝试对其进行编译时,出现错误“android sdk被错误地解析”。有什么帮助吗? 参考方案 尝试将Android Studio配置为指向您的SDK安装。
首先关注EditText清晰文本-Android - javaIt's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current fo…
Java:正则表达式模式匹配器是否有大小限制? - java我的模式类似于OR:“word1 | word2 | word3”我大约有800个字。可能有问题吗? 参考方案 您仅受记忆和理智的限制。 :)