Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Admob InterstitialAd Deprecated

After updating com.google.android.gms:play-services-ads to 19.7.0 it says InterstitialAd & RewardedVideoAd & UnifiedNativeAdView deprecated.

can anyone help ?

like image 615
Mohammad Sommakia Avatar asked Jan 24 '21 20:01

Mohammad Sommakia


People also ask

What is Interstitialad?

Interstitial ads provide rich interactive ads for users on mobile apps. Interstitial ads are designed to be placed between content, so they are best placed at natural app transition points. AdMob publishers should carefully implement interstitial ads to provide good user experiences and avoid accidental clicks.

How much do interstitial ads pay AdMob?

How much do interstitial ads pay? Interstitial ads have one of the highest CPM prices compared to other ad formats like banner ads. For example, in 2021, mobile interstitial ads in the U.S. generated the highest eCPM rates for publishers–$9.64 on iOS and $10.11 on Android.

How do I close an interstitial ad programmatically?

dispatchKeyEvent(new KeyEvent(KeyEvent. 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.


1 Answers

after a quick search, I found that we need to use

public abstract class InterstitialAd extends Object

instead of

public final class InterstitialAd extends Object

so you need to use:

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

instead of:

come.google.android.gms.ads

and this is how we interact with the new package:

public class AdManager 
{
    private var interstitialAd: InterstitialAd? = null
    private var mRewardedVideoAd: RewardedAd? = null
    private var currentNativeAd: NativeAd? = null
    private var builder: AdLoader.Builder? = null



    init {
        RequestConfiguration.Builder().setTestDeviceIds(
            listOf(
                AdRequest.DEVICE_ID_EMULATOR,
                "CE88B9A1CB213EEEA19A2F7E54993908"
            )
        )
    }
    // Create a full screen content callback.
    val fullScreenContentCallback = object : FullScreenContentCallback() {
        override fun onAdFailedToShowFullScreenContent(p0: AdError) {
            super.onAdFailedToShowFullScreenContent(p0)
        }

        override fun onAdShowedFullScreenContent() {
            super.onAdShowedFullScreenContent()
        }

        override fun onAdDismissedFullScreenContent() {
            super.onAdDismissedFullScreenContent()
            interstitialAd = null
            mRewardedVideoAd = null
        }

    }

    fun createInterstitialAd(context: Context) {
     
            InterstitialAd.load(
                context,
                BuildConfig.ADMOB_AD_INTERSTITIAL_UNIT_ID,
                request.build(),
                object : InterstitialAdLoadCallback() {
                    override fun onAdLoaded(ad: InterstitialAd) {
                        interstitialAd = ad
                        interstitialAd?.fullScreenContentCallback = fullScreenContentCallback
                    }

                })
        }
    }

    fun getFullScreenAd(): InterstitialAd? {
        return interstitialAd
    }

    fun getVideoAd(): RewardedAd? {
        return mRewardedVideoAd
    }

    fun loadRewardedVideoAd(context: Context) {
        if (!userManager.getCurrentUser().isPremium) {
            val request = AdRequest.Builder()
     

            RewardedAd.load(
                context,
                BuildConfig.ADMOB_AD_VIDEO_UNIT_ID,
                AdRequest.Builder().build(),
                object : RewardedAdLoadCallback() {
                    override fun onAdLoaded(ad: RewardedAd) {
                        super.onAdLoaded(ad)
                        mRewardedVideoAd = ad;
                        mRewardedVideoAd?.fullScreenContentCallback = fullScreenContentCallback;
                    }
                    override fun onAdFailedToLoad(p0: LoadAdError) {
                        super.onAdFailedToLoad(p0)

                    }
                })
        }
   

    fun loadNativeAd(context: Activity,adFrame:FrameLayout) {
      
            builder = AdLoader.Builder(context, BuildConfig.ADMOB_AD_UNIT_ID_DIALOG_NATIVE)
            builder?.forNativeAd { unifiedNativeAd: NativeAd ->
                val adView: View =
                    context.layoutInflater.inflate(R.layout.ad_unified, null)
                val ad = adView as NativeAdView
                populateUnifiedNativeAdView(unifiedNativeAd, ad)
                adFrame.removeAllViews()
                adFrame.addView(ad)
            }
            val adLoader = builder?.withAdListener(object : AdListener() {
                override fun onAdFailedToLoad(i: LoadAdError) {
                    super.onAdFailedToLoad(i)
                    Log.e("NativeAdFailed", i.toString() + "")
                }
            })?.build()
            val builder = AdManagerAdRequest.Builder()
            adLoader?.loadAd(builder.build())
        }
    }


    private fun populateUnifiedNativeAdView(
        nativeAd: NativeAd,
        adView: NativeAdView
    ) {
        // You must call destroy on old ads when you are done with them,
        // otherwise you will have a memory leak.
        if (currentNativeAd != null) currentNativeAd?.destroy()
        currentNativeAd = nativeAd

        // Set the media view.
        adView.mediaView = adView.findViewById<View>(R.id.ad_media) as com.google.android.gms.ads.nativead.MediaView

        // Set other ad assets.
        adView.headlineView = adView.findViewById(R.id.ad_headline)
        adView.bodyView = adView.findViewById(R.id.ad_body)
        adView.callToActionView = adView.findViewById(R.id.ad_call_to_action)
        adView.iconView = adView.findViewById(R.id.ad_app_icon)
        adView.priceView = adView.findViewById(R.id.ad_price)
        adView.starRatingView = adView.findViewById(R.id.ad_stars)
        adView.storeView = adView.findViewById(R.id.ad_store)
        adView.advertiserView = adView.findViewById(R.id.ad_advertiser)

        // The headline and media content are guaranteed to be in every UnifiedNativeAd.
        (adView.headlineView as TextView).text = nativeAd.headline
        nativeAd.mediaContent?.let {
            adView.mediaView?.setMediaContent(it)

        }

        // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
        // check before trying to display them.
        if (nativeAd.body == null) {
            adView.bodyView?.visibility = View.INVISIBLE
        } else {
            adView.bodyView?.visibility = View.VISIBLE
            (adView.bodyView as TextView).text = nativeAd.body
        }
        if (nativeAd.callToAction == null) {
            adView.callToActionView?.visibility = View.INVISIBLE
        } else {
            adView.callToActionView?.visibility = View.VISIBLE
            (adView.callToActionView as TextView).text = nativeAd.callToAction
        }
        if (nativeAd.icon == null) {
            adView.iconView?.visibility = View.GONE
        } else {
            (adView.iconView as ImageView).setImageDrawable(
                nativeAd.icon?.drawable
            )
            adView.iconView?.visibility = View.VISIBLE
        }
        if (nativeAd.price == null) {
            adView.priceView?.visibility = View.INVISIBLE
        } else {
            adView.priceView?.visibility = View.VISIBLE
            (adView.priceView as TextView).text = nativeAd.price
        }
        if (nativeAd.store == null) {
            adView.storeView?.visibility = View.INVISIBLE
        } else {
            adView.storeView?.visibility = View.VISIBLE
            (adView.storeView as TextView).text = nativeAd.store
        }
        if (nativeAd.starRating == null) {
            adView.starRatingView?.visibility = View.INVISIBLE
        } else {
            nativeAd.starRating?.toDouble()?.let {
                (adView.starRatingView as RatingBar).rating = it.toFloat()
                adView.starRatingView?.visibility = View.VISIBLE
            }


        }
        if (nativeAd.advertiser == null) {
            adView.advertiserView?.visibility = View.INVISIBLE
        } else {
            (adView.advertiserView as TextView).text = nativeAd.advertiser
            adView.advertiserView?.visibility = View.VISIBLE
        }

        // This method tells the Google Mobile Ads SDK that you have finished populating your
        // native ad view with this native ad.
        adView.setNativeAd(nativeAd)
    }
}
like image 175
Mohammad Sommakia Avatar answered Oct 09 '22 00:10

Mohammad Sommakia