「Android Mopub Banner」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser |
imported>Wikiuser |
||
| 行 33: | 行 33: | ||
@Override | @Override | ||
public void onReceiveAd(TWMAd ad) { | public void onReceiveAd(TWMAd ad) { | ||
customEventBannerListener.onBannerLoaded(adView); | if(customEventBannerListener != null) | ||
customEventBannerListener.onBannerLoaded(adView); | |||
} | } | ||
@Override | @Override | ||
public void onFailedToReceiveAd(TWMAd ad, TWMAdRequest.ErrorCode errorCode) { | public void onFailedToReceiveAd(TWMAd ad, TWMAdRequest.ErrorCode errorCode) { | ||
customEventBannerListener.onBannerFailed(MoPubErrorCode.NETWORK_NO_FILL); | if(customEventBannerListener != null) | ||
customEventBannerListener.onBannerFailed(MoPubErrorCode.NETWORK_NO_FILL); | |||
} | } | ||
@Override | @Override | ||
public void onPresentScreen(TWMAd ad) { | public void onPresentScreen(TWMAd ad) { | ||
customEventBannerListener.onBannerClicked(); | if(customEventBannerListener != null) | ||
customEventBannerListener.onBannerClicked(); | |||
} | } | ||
| 行 51: | 行 54: | ||
@Override | @Override | ||
public void onLeaveApplication(TWMAd ad) { | public void onLeaveApplication(TWMAd ad) { | ||
customEventBannerListener.onLeaveApplication(); | if(customEventBannerListener != null) | ||
customEventBannerListener.onLeaveApplication(); | |||
} | } | ||
| 行 65: | 行 69: | ||
Views.removeFromParent(adView); | Views.removeFromParent(adView); | ||
if (adView != null) { | if (adView != null) { | ||
adView.setAdListener(null); | |||
adView.destroy(); | adView.destroy(); | ||
} | } | ||
} | } | ||
} | } | ||
<br><br> | <br><br> | ||
[[Android Integeration with Mopub Mediation|回MoPub教學]]<br> | [[Android Integeration with Mopub Mediation|回MoPub教學]]<br> | ||
於 2017年9月6日 (三) 03:54 的修訂
請建立一個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();
}
}
}