In the google play console - I keep getting ANR caused by google Interstitial ads. the application probably freeze for 0.5 seconds or more.
Broadcast of Intent { act=android.intent.action.SCREEN_ON flg=0x50200010 }, InvisibleToUser
Should I use thread somewhere? this is the code that I am using to init the ads:
'''
mAdView = findViewById(R.id.ad_view);
MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxx");
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-123123123123461/1231236530");
mInterstitialAd.setAdListener(new AdListener()
{
@Override
public void onAdLoaded()
{
//log interstitial loaded
}
@Override
public void onAdFailedToLoad(int errorCode)
{
}
@Override
public void onAdClosed()
{
//reload interstitial
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
// Ad interstitialAd reLoaded
}
});
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
'''
this is the stack submitted in the google play console
"main" tid=1 Runnable
"main" prio=5 tid=1 Runnable
| group="main" sCount=0 dsCount=0 flags=0 obj=0x728685e0 self=0xb0384000
| sysTid=18461 nice=0 cgrp=default sched=0/0 handle=0xb3dbd4a8
| state=R schedstat=( 1710859708561 515750767270 4655929 ) utm=110119 stm=60966 core=1 HZ=100
| stack=0xbe79b000-0xbe79d000 stackSize=8MB
| held mutexes= "mutator lock"(shared held)
at java.util.Arrays.asList (Arrays.java:3728)
at dalvik.system.DexPathList.findClass (DexPathList.java:473)
at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:91)
at java.lang.ClassLoader.loadClass (ClassLoader.java:379)
at java.lang.ClassLoader.loadClass (ClassLoader.java:312)
at com.google.android.gms.ads.internal.util.m.l (m.java:3)
at com.google.android.gms.ads.ChimeraAdManagerCreatorImpl.newAdManagerByType (ChimeraAdManagerCreatorImpl.java:4)
at com.google.android.gms.ads.internal.client.at.a (at.java)
at jy.onTransact (jy.java:3)
at android.os.Binder.transact (Binder.java:604)
at csk.a (csk.java:2)
at com.google.android.gms.ads.internal.client.o.newAdManagerByType (o.java)
at com.google.android.gms.ads.AdManagerCreatorImpl.newAdManagerByType (AdManagerCreatorImpl.java:6)
at com.google.android.gms.ads.internal.client.l.a (l.java:18)
at csj.onTransact (csj.java:3)
at android.os.Binder.transact (Binder.java:604)
at com.google.android.gms.internal.ads.zzfm.transactAndReadException (zzfm.java:10)
at com.google.android.gms.internal.ads.zzzm.zza (zzzm.java:10)
at com.google.android.gms.internal.ads.zzxv.zza (zzxv.java:6)
at com.google.android.gms.internal.ads.zzyj.zzow (zzyj.java:7)
at com.google.android.gms.internal.ads.zzyq.zzoz (zzyq.java:29)
at com.google.android.gms.internal.ads.zzyq.zzd (zzyq.java:54)
at com.google.android.gms.internal.ads.zzabb.zza (zzabb.java:40)
at com.google.android.gms.ads.InterstitialAd.loadAd (InterstitialAd.java:9)
at com.arbelsolutions.BVRUltimate.GalleryActivity.ConfigAds (GalleryActivity.java:156)
at com.arbelsolutions.BVRUltimate.GalleryActivity.acknowledgedPurchase (GalleryActivity.java:301)
at com.arbelsolutions.BVRUltimate.PlayDonateClient.onBillingSetupFinished (PlayDonateClient.java:101)
at com.android.billingclient.api.BillingClientImpl$BillingServiceConnection$1.run (BillingClientImpl.java:1521)
- locked <0x0ddd2f0b> (a java.lang.Object)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6592)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:769)
Following Saxenarishav question, I will post the complete code that made this problem never to repeat. First: it was the admobs preference update in the dependencies. Second was to check before loading if the ad is already loaded. But most important is making the initialization on thread.
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequestInterstitial = null;
adRequestInterstitial = new AdRequest.Builder().build();
final AdRequest guiAdRequestInterstitial = adRequestInterstitial;
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!mInterstitialAd.isLoaded()) {
mInterstitialAd.loadAd(guiAdRequestInterstitial);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdFailedToLoad(int errorCode) {
Log.d(TAG, "onAdFailedToLoad : errorcode : " + String.valueOf(errorCode));
super.onAdFailedToLoad(errorCode);
}
@Override
public void onAdClosed() {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
// Ad interstitialAd reLoaded
Log.d(TAG, "Ad interstitialAd reLoaded");
super.onAdClosed();
}
});
Log.d(TAG, "ads initialize");
}
}
});
Edit II : to prepare for version 20.0.0 of admobs - some changes were made: https://developers.google.com/admob/android/interstitial-fullscreen
I am posting here the new solution. it runs for the last 2 monthes on half a million users - until now - without ANR.
private void SetInterstitial() {
String interadUnit = "ca-app-pub-3940256099942544/1033173712";
mAdRequestInterstitialAd = new AdRequest.Builder().
.addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle())
.build();
InterstitialAd.load(GalleryActivity.this,interadUnit, mAdRequestInterstitialAd, new InterstitialAdLoadCallback() {//getString(R.string.Interstitial)
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
mInterstitialAd=null;
SetInterstitial();
Log.d(TAG, "perform your code that you wants todo after ad dismissed or closed");
}
@Override
public void onAdFailedToShowFullScreenContent(com.google.android.gms.ads.AdError adError) {
super.onAdFailedToShowFullScreenContent(adError);
mInterstitialAd = null;
Log.d(TAG, "perform your action here when ad will not load");
}
@Override
public void onAdShowedFullScreenContent() {
super.onAdShowedFullScreenContent();
Log.d(TAG, "onAdShowedFullScreenContent");
mInterstitialAd = null;
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
mInterstitialAd = null;
Log.e(TAG, "Adsmobs:Interstitial:loadAdError:" + loadAdError);
if (adsInterstitialFailCounter++ < 4)
SetInterstitial();
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With