After updating Google Ads SDK InterstitialAd is deprecated, How to resolve?

Viewed 2674

After updating Google Ads SDK to 19.7.0 gives a deprecated warning message for InterstitialAd, while I searched this link for resolving the issue but not succeed. how can I resolve it?

Here my code

public void InterstitialAdmob() {
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(Util.ADMOBINTER);
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
        }
    });
    requestNewInterstitial();
}

protected void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder().addTestDevice(ADMOBDEV).build();
    mInterstitialAd.loadAd(adRequest);
}
// for showing ads
 if (mInterstitialAd.isLoaded()) {
      mInterstitialAd.show();
   }

and developer site or suggestion

3 Answers

This is what I did on my fragment, with just 4 steps.

1.Replace the deprecated import:

import com.google.android.gms.ads.InterstitialAd

with the new one:

import com.google.android.gms.ads.interstitial.InterstitialAd

2.Replace the old initialization:

    interstitialAd = InterstitialAd(requireContext())
    interstitialAd.adUnitId = "ca-app-pub-00000000/11111111"
    interstitialAd.loadAd(AdRequest.Builder().build())

with the new one:

    val adRequest = AdRequest.Builder().build()
    InterstitialAd.load(requireContext(),
        "ca-app-pub-00000000/11111111",
         adRequest,
            object : InterstitialAdLoadCallback() {
                override fun onAdLoaded(myAd: InterstitialAd) {
                    Timber.d("Ad Loaded")
                    interstitialAd = myAd
                }

                override fun onAdFailedToLoad(adError: LoadAdError) {
                    Timber.d("Failed to load ad: ${adError.message}")
                    interstitialAd = null
                }
            })

3.Replace the old way of showing it:

    if (interstitialAd.isLoaded) {
        interstitialAd.show()
    } else {
         Timber.d("Ad wasn't loaded yet!")
    }

with the new one:

    if (interstitialAd != null){
        interstitialAd?.show(requireActivity())
    }else {
        Timber.d("Interstitial Ad not ready yet")
    }

4.There is no step 4, enjoy

Source

public static com.google.android.gms.ads.interstitial.InterstitialAd googleFullscreen;


public static void mLoadGoogleFullScreenAds(final Activity activity) {
    FullScreenContentCallback fullScreenContentCallback = new FullScreenContentCallback() {
        @Override
        public void onAdDismissedFullScreenContent() {
            googleFullscreen = null;
            // Proceed to the next level.
        }

        @Override
        public void onAdShowedFullScreenContent() {
            super.onAdShowedFullScreenContent();
        }

        @Override
        public void onAdFailedToShowFullScreenContent(com.google.android.gms.ads.AdError adError) {
            super.onAdFailedToShowFullScreenContent(adError);
            Log.d(TAG, "onAdFailedToShowFullScreenContent: " + adError.toString());
        }
    };
    googleFullscreen.load(
            activity,
            activity.getResources().getString(R.string.google_fullscreen),
            new AdRequest.Builder().build(),
            new InterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull InterstitialAd ad) {
                    googleFullscreen = ad;googleFullscreen.setFullScreenContentCallback(fullScreenContentCallback);
                    googleFullscreen.show(activity);
                    }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError adError) {
                    googleFullscreen = null;
                    Log.d(TAG, "onAdFailedToLoad: " + adError.toString());
                    // Code to be executed when an ad request fails.
                }
            });
}

i think this code solve your problem

Related