Android Mopub Interstitial SDK8
於 2021年8月31日 (二) 07:53 由 imported>Wikiuser 所做的修訂 (新頁面: 請建立一個Class並繼承Mopub所提供的BaseAd<br><br> Class實作內容如下:<br> <source> package com.taiwanmobile.pt.adp.mediation import android.app.Activity import andr...)
請建立一個Class並繼承Mopub所提供的BaseAd
Class實作內容如下:
package com.taiwanmobile.pt.adp.mediation
import android.app.Activity
import android.content.Context
import com.mopub.common.LifecycleListener
import com.mopub.common.logging.MoPubLog
import com.mopub.mobileads.AdData
import com.mopub.mobileads.BaseAd
import com.mopub.mobileads.MoPubErrorCode
import com.taiwanmobile.pt.adp.view.TWMAd
import com.taiwanmobile.pt.adp.view.TWMAdRequest
import com.taiwanmobile.pt.adp.view.TWMAdViewListener
import com.taiwanmobile.pt.adp.view.TWMInterstitialAd
// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubInterstitial 填入後台的Custom Event Class欄位
class TAMediaMopubInterstitial : BaseAd() {
private var adUnitId = ""
private var interstitialAd: TWMInterstitialAd? = null
override fun onInvalidate() {
// Called when MoPubInterstitial is being invalidated or destroyed.
interstitialAd?.setAdListener(null)
}
override fun getLifecycleListener(): LifecycleListener? {
return null
}
override fun getAdNetworkId(): String {
return adUnitId
}
override fun checkAndInitializeSdk(launcherActivity: Activity, adData: AdData): Boolean {
return false
}
override fun load(context: Context, adData: AdData) {
try {
setAutomaticImpressionAndClickTracking(false)
adUnitId = adData.extras.getValue(KEY_AD_UNIT_ID)
interstitialAd = TWMInterstitialAd(context as Activity, adUnitId)
interstitialAd?.setAdListener(object : TWMAdViewListener {
override fun onReceiveAd(ad: TWMAd) {
mLoadListener?.onAdLoaded()
interstitialAd?.show()
}
override fun onFailedToReceiveAd(ad: TWMAd, errorCode: TWMAdRequest.ErrorCode) {
mLoadListener?.onAdLoadFailed(MoPubErrorCode.NO_FILL)
}
override fun onPresentScreen(ad: TWMAd) {
mInteractionListener?.onAdImpression()
}
override fun onDismissScreen(ad: TWMAd) {
mInteractionListener?.onAdDismissed()
}
override fun onLeaveApplication(ad: TWMAd) {
mInteractionListener?.onAdClicked()
}
})
interstitialAd?.loadAd(TWMAdRequest())
} catch (e: Exception) {
MoPubLog.log(
adNetworkId, MoPubLog.AdapterLogEvent.LOAD_FAILED, javaClass.name,
MoPubErrorCode.NETWORK_NO_FILL.intCode, MoPubErrorCode.NETWORK_NO_FILL)
mLoadListener?.onAdLoadFailed(MoPubErrorCode.NETWORK_NO_FILL)
e.printStackTrace()
}
}
companion object {
private const val KEY_AD_UNIT_ID = "adUnitId"
}
}