Created
December 13, 2022 17:09
-
-
Save MrNtlu/71b627322c50cbe02964e86550a3a513 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mInterstitialAd: InterstitialAd? = null | |
fun loadInterstitial(context: Context) { | |
InterstitialAd.load( | |
context, | |
"ca-app-pub-3940256099942544/1033173712", //Change this with your own AdUnitID! | |
AdRequest.Builder().build(), | |
object : InterstitialAdLoadCallback() { | |
override fun onAdFailedToLoad(adError: LoadAdError) { | |
mInterstitialAd = null | |
} | |
override fun onAdLoaded(interstitialAd: InterstitialAd) { | |
mInterstitialAd = interstitialAd | |
} | |
} | |
) | |
} | |
fun showInterstitial(context: Context, onAdDismissed: () -> Unit) { | |
val activity = context.findActivity() | |
if (mInterstitialAd != null && activity != null) { | |
mInterstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() { | |
override fun onAdFailedToShowFullScreenContent(e: AdError) { | |
mInterstitialAd = null | |
} | |
override fun onAdDismissedFullScreenContent() { | |
mInterstitialAd = null | |
loadInterstitial(context) | |
onAdDismissed() | |
} | |
} | |
mInterstitialAd?.show(activity) | |
} | |
} | |
fun removeInterstitial() { | |
mInterstitialAd?.fullScreenContentCallback = null | |
mInterstitialAd = null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment