Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 773
Attaullah Avatar asked Jan 25 '21 07:01

Attaullah


People also ask

Is AdMob deprecated?

On October 12, 2021, AdMob metrics will be deprecated in the AdSense API.

How do I initialize Google mobile ad SDK?

Before loading ads, have your app initialize the Google Mobile Ads SDK by calling MobileAds. initialize() which initializes the SDK and calls back a completion listener once initialization is complete (or after a 30-second timeout). This needs to be done only once, ideally at app launch.

How do I close an interstitial ad programmatically?

ACTION_DOWN, KeyEvent. KEYCODE_BACK)); As whenever you press on back button the interstitial ad is closed so firing the back button event will eventually close the interstitial ad.


2 Answers

Check the new API examples here: https://developers.google.com/admob/android/interstitial-fullscreen

Warning: There are many breaking changes coming in version 20.0.0. Version 19.7.0 introduces many new APIs, and deprecates or renames many classes in preparation for version 20.0.0. Please read the migration guide for more details on the changes.

https://developers.google.com/admob/android/migration

like image 192
lubosz Avatar answered Sep 25 '22 02:09

lubosz


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

like image 36
Doilio Matsinhe Avatar answered Sep 26 '22 02:09

Doilio Matsinhe