「Android Mopub Banner」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
 
行 18: 行 18:
   import java.util.Map;
   import java.util.Map;
    
    
   '''//需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubBanner 填入後台的Custom Event Class欄位'''
   // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubBanner 填入後台的Custom Event Class欄位
   public class TAMediaMopubBanner extends CustomEventBanner {
   public class TAMediaMopubBanner extends CustomEventBanner {
       private static final String TAG = "TAMediaMopubBanner";
       private static final String TAG = "TAMediaMopubBanner";
      private static final String KEY_MOPUB_WIDTH = "com_mopub_ad_width";
      private static final String KEY_MOPUB_HEIGHT = "com_mopub_ad_height";
    
    
       private TWMAdView adView = null;
       private TWMAdView adView = null;
行 28: 行 30:
           Log.d(TAG, "loadBanner(" + serverExtras.get("adUnitId") + ") invoked!!");
           Log.d(TAG, "loadBanner(" + serverExtras.get("adUnitId") + ") invoked!!");
    
    
           String adUnitId = serverExtras.get("adUnitId"); '''//後台設定的adUnitId會透過此Key值取得'''
          int width = 0, height = 0;
           adView = new TWMAdView( (Activity)context, TWMAdSize.SMART_BANNER, adUnitId );
          if(localExtras.containsKey(KEY_MOPUB_WIDTH) && localExtras.containsKey(KEY_MOPUB_HEIGHT)){
              width = (int)localExtras.get(KEY_MOPUB_WIDTH);
              height = (int)localExtras.get(KEY_MOPUB_HEIGHT);
          }
 
           String adUnitId = serverExtras.get("adUnitId"); //後台設定的adUnitId會透過此Key值取得
           adView = new TWMAdView( (Activity)context, checkAdSize(width, height), adUnitId );
           adView.setAdListener(new TWMAdViewListener(){
           adView.setAdListener(new TWMAdViewListener(){
               @Override
               @Override
行 69: 行 77:
               adView.destroy();
               adView.destroy();
           }
           }
      }
 
      private TWMAdSize checkAdSize(int width, int height) {
          TWMAdSize adSize;
          if(width == TWMAdSize.BANNER.getWidth() && height == TWMAdSize.BANNER.getHeight()) {
              adSize = TWMAdSize.BANNER;
          }else if(width == TWMAdSize.IAB_MRECT.getWidth() && height == TWMAdSize.IAB_MRECT.getHeight()) {
              adSize = TWMAdSize.IAB_MRECT;
          }else {
              adSize = TWMAdSize.BANNER;
          }
 
          return adSize;
       }
       }
   }
   }


<br><br>
<br><br>
[[Android Integeration with Mopub Mediation|回MoPub教學]]<br>
[[Android Integeration with Mopub Mediation|回MoPub教學]]<br>

於 2018年8月20日 (一) 08:50 的最新修訂

請建立一個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 static final String KEY_MOPUB_WIDTH = "com_mopub_ad_width";
     private static final String KEY_MOPUB_HEIGHT = "com_mopub_ad_height";
 
     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!!");
 
         int width = 0, height = 0;
         if(localExtras.containsKey(KEY_MOPUB_WIDTH) && localExtras.containsKey(KEY_MOPUB_HEIGHT)){
             width = (int)localExtras.get(KEY_MOPUB_WIDTH);
             height = (int)localExtras.get(KEY_MOPUB_HEIGHT);
         }
 
         String adUnitId = serverExtras.get("adUnitId"); //後台設定的adUnitId會透過此Key值取得
         adView = new TWMAdView( (Activity)context, checkAdSize(width, height), 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) {}
 
         });
         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();
         }
     }
 
     private TWMAdSize checkAdSize(int width, int height) {
         TWMAdSize adSize;
         if(width == TWMAdSize.BANNER.getWidth() && height == TWMAdSize.BANNER.getHeight()) {
             adSize = TWMAdSize.BANNER;
         }else if(width == TWMAdSize.IAB_MRECT.getWidth() && height == TWMAdSize.IAB_MRECT.getHeight()) {
             adSize = TWMAdSize.IAB_MRECT;
         }else {
             adSize = TWMAdSize.BANNER;
         }
 
         return adSize;
     }
 }




回MoPub教學