Android Mopub Interstitial SDK8
於 2021年8月31日 (二) 07:55 由 imported>Wikiuser 所做的修訂
請建立一個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"
}
}
MoPub後台設定
- 將上述class完整package設定置MoPub Custom event class
- 將TAmedia所提供的adUnitId設定於MoPub Custom event data(必要), MEDIA_PREFER_IMAGE與VIDEO_START_UNMUTED則為非必要參數