5

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

Attaullah
  • 3,856
  • 3
  • 48
  • 63

3 Answers3

5

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

lubosz
  • 848
  • 10
  • 12
4

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

Doilio Matsinhe
  • 2,131
  • 16
  • 29
0
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

Mihir Akoliya
  • 166
  • 1
  • 7