Skip to content

Instantly share code, notes, and snippets.

@hoanghiephui
Created March 31, 2021 08:10
Show Gist options
  • Save hoanghiephui/f1dfe6741695990a88a24867f7fdd7e2 to your computer and use it in GitHub Desktop.
Save hoanghiephui/f1dfe6741695990a88a24867f7fdd7e2 to your computer and use it in GitHub Desktop.
Mediation Mopub Interstitial
class AdFullLoader : MoPubInterstitial.InterstitialAdListener, InterstitialAdListener {
private var interstitialAd: InterstitialAd? = null
private var interstitialAdMoPub: MoPubInterstitial? = null
fun loadAds(
activity: Activity,
adUnitId: String,
isUseMediation: Boolean
) {
if (isUseMediation) {
if (interstitialAdMoPub == null) {
val facebookConfig = HashMap<String, Any>()
facebookConfig["interstitial"] = true
interstitialAdMoPub =
MoPubInterstitial(
activity,
adUnitId
).apply {
interstitialAdListener = this@AdFullLoader
setLocalExtras(facebookConfig)
load()
}
}
} else {
if (interstitialAd == null) {
interstitialAd = InterstitialAd(activity, adUnitId)
interstitialAd?.let {
it.loadAd(
it.buildLoadAdConfig()
.withAdListener(this)
.build()
)
}
}
}
}
fun showAdsFull() {
if (interstitialAdMoPub != null) {
if (interstitialAdMoPub?.isReady == true) {
interstitialAdMoPub?.show()
}
} else if (interstitialAd != null) {
if (interstitialAd?.isAdLoaded == true) {
interstitialAd?.show()
}
}
}
override fun onInterstitialLoaded(interstitial: MoPubInterstitial?) {}
override fun onInterstitialFailed(
interstitial: MoPubInterstitial?,
errorCode: MoPubErrorCode?
) {
}
override fun onInterstitialShown(interstitial: MoPubInterstitial?) {
}
override fun onInterstitialClicked(interstitial: MoPubInterstitial?) {
}
override fun onInterstitialDismissed(interstitial: MoPubInterstitial?) {
interstitialAdMoPub?.destroy()
interstitialAdMoPub = null
}
override fun onError(p0: Ad?, p1: AdError?) {
}
override fun onAdLoaded(p0: Ad?) {
}
override fun onAdClicked(p0: Ad?) {
}
override fun onLoggingImpression(p0: Ad?) {
}
override fun onInterstitialDisplayed(p0: Ad?) {
}
override fun onInterstitialDismissed(p0: Ad?) {
interstitialAd?.destroy()
interstitialAd = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment