Android Mopub Banner
於 2017年9月6日 (三) 03:54 由 imported>Wikiuser 所做的修訂
請建立一個Class並繼承Mopub所提供的CustomEventBanner
Class實作內容如下:
package com.taiwanmobile.pt.adp.mediation;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import com.mopub.common.util.Views;
import com.mopub.mobileads.CustomEventBanner;
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.TWMAdSize;
import com.taiwanmobile.pt.adp.view.TWMAdView;
import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
import java.util.Map;
//需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubBanner 填入後台的Custom Event Class欄位
public class TAMediaMopubBanner extends CustomEventBanner {
private static final String TAG = "TAMediaMopubBanner";
private TWMAdView adView = null;
@Override
protected void loadBanner(Context context, final CustomEventBannerListener customEventBannerListener, Map<String, Object> localExtras, Map<String, String> serverExtras) {
Log.d(TAG, "loadBanner(" + serverExtras.get("adUnitId") + ") invoked!!");
String adUnitId = serverExtras.get("adUnitId"); //後台設定的adUnitId會透過此Key值取得
adView = new TWMAdView( (Activity)context, TWMAdSize.SMART_BANNER, adUnitId );
adView.setAdListener(new TWMAdViewListener(){
@Override
public void onReceiveAd(TWMAd ad) {
if(customEventBannerListener != null)
customEventBannerListener.onBannerLoaded(adView);
}
@Override
public void onFailedToReceiveAd(TWMAd ad, TWMAdRequest.ErrorCode errorCode) {
if(customEventBannerListener != null)
customEventBannerListener.onBannerFailed(MoPubErrorCode.NETWORK_NO_FILL);
}
@Override
public void onPresentScreen(TWMAd ad) {
if(customEventBannerListener != null)
customEventBannerListener.onBannerClicked();
}
@Override
public void onDismissScreen(TWMAd ad) {}
@Override
public void onLeaveApplication(TWMAd ad) {
if(customEventBannerListener != null)
customEventBannerListener.onLeaveApplication();
}
});
adView.loadAd(new TWMAdRequest());
}
@Override
protected void onInvalidate() {
// Called when MoPubView is being invalidated or destroyed
Log.e(TAG, "onInvalidate invoke !!");
Views.removeFromParent(adView);
if (adView != null) {
adView.setAdListener(null);
adView.destroy();
}
}
}