「Android Mopub Native」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
行 2: 行 2:
Class實作內容如下:<br>
Class實作內容如下:<br>
   package com.taiwanmobile.pt.adp.mediation;
   package com.taiwanmobile.pt.adp.mediation;
 
 
   import android.app.Activity;
   import android.app.Activity;
   import android.content.Context;
   import android.content.Context;
   import android.util.Log;
   import android.util.Log;
   import android.view.View;
   import android.view.View;
 
 
   import com.mopub.nativeads.CustomEventNative;
   import com.mopub.nativeads.CustomEventNative;
   import com.mopub.nativeads.NativeErrorCode;
   import com.mopub.nativeads.NativeErrorCode;
行 20: 行 20:
   import java.util.Map;
   import java.util.Map;
    
    
   '''// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubNative 填入後台的Custom Event Class欄位'''
   <b>// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubNative 填入後台的Custom Event Class欄位</b>
   public class TAMediaMopubNative extends CustomEventNative {
   public class TAMediaMopubNative extends CustomEventNative {
       private static final String TAG = "TAMediaMopubNative";
       private static final String TAG = "TAMediaMopubNative";
行 32: 行 32:
                                   final Map<String, String> serverExtras) {
                                   final Map<String, String> serverExtras) {
           Log.d(TAG, "loadNativeAd(" + serverExtras.get("adUnitId") + ") invoked!!");
           Log.d(TAG, "loadNativeAd(" + serverExtras.get("adUnitId") + ") invoked!!");
 
           String adUnitId = serverExtras.get("adUnitId"); //後台設定的adUnitId會透過此Key值取得
           String adUnitId = serverExtras.get("adUnitId"); '''//後台設定的adUnitId會透過此Key值取得'''
           nativeAd = new TWMNativeAd((Activity) context, adUnitId);
           nativeAd = new TWMNativeAd((Activity) context, adUnitId);
           final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(nativeAd, customEventNativeListener);
           final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(nativeAd, customEventNativeListener);
           nativeAd.setAdListener(staticNativeAd);
           nativeAd.setAdListener(staticNativeAd);
 
          //String testId = Utility.getEncodedAndroidId(context);
           nativeAd.loadAd(new TWMAdRequest());
           nativeAd.loadAd(new TWMAdRequest());
       }
       }
行 51: 行 52:
           private String imageUrl;
           private String imageUrl;
           private String targetUrl;
           private String targetUrl;
          private String callToAction;
    
    
           public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) {
           public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) {
               mCustomEventNativeListener = customEventNativeListener;
               mCustomEventNativeListener = customEventNativeListener;
               nativeAd = nativeAD;
               nativeAd = nativeAD;
          }
 
          @Override
          public void prepare(final View view) {
              super.prepare(view);
 
              // Set click event to all view.
              view.setOnClickListener(clickListener);
          }
 
          @Override
          public void clear(View view) {
              view.setOnClickListener(null);
              super.clear(view);
           }
           }
    
    
行 61: 行 77:
               nativeAd.destroy();
               nativeAd.destroy();
               super.destroy();
               super.destroy();
          }
          @Override
          public void prepare(View view) {
              super.prepare(view);
 
              view.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      notifyAdClicked();
                      nativeAd.handleClick();
                  }
              });
 
           }
           }
    
    
行 87: 行 90:
                       imageUrl = (content.has("IMAGE1200X627")) ? content.getString("IMAGE1200X627") : null;
                       imageUrl = (content.has("IMAGE1200X627")) ? content.getString("IMAGE1200X627") : null;
                       targetUrl = (content.has("nurl")) ? content.getString("nurl") : null;
                       targetUrl = (content.has("nurl")) ? content.getString("nurl") : null;
                      callToAction = (content.has("CTA")) ? content.getString("CTA") : null;
                       if(longSubject != null) setTitle(longSubject);
                       if(longSubject != null) setTitle(longSubject);
                       if(body != null) setText(body);
                       if(body != null) setText(body);
行 92: 行 96:
                       if(iconUrl != null) setIconImageUrl(iconUrl);
                       if(iconUrl != null) setIconImageUrl(iconUrl);
                       if(targetUrl != null) setClickDestinationUrl(targetUrl);
                       if(targetUrl != null) setClickDestinationUrl(targetUrl);
                      if(callToAction != null) setCallToAction(callToAction);
    
    
                       if(mCustomEventNativeListener != null)
                       if(mCustomEventNativeListener != null)
行 112: 行 117:
           @Override
           @Override
           public void onLeaveApplication(TWMAd twmAd) {}
           public void onLeaveApplication(TWMAd twmAd) {}
 
          private View.OnClickListener clickListener = new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  notifyAdClicked();
                  nativeAd.handleClick();
              }
          };
 
       }
       }
 
   }
   }
<br><br>
<br><br>
[[Android Integeration with Mopub Mediation|回MoPub教學]]<br>
[[Android Integeration with Mopub Mediation|回MoPub教學]]<br>

於 2018年6月11日 (一) 03:34 的修訂

請建立一個Class並繼承Mopub所提供的CustomEventNative

Class實作內容如下:

 package com.taiwanmobile.pt.adp.mediation;
  
 import android.app.Activity;
 import android.content.Context;
 import android.util.Log;
 import android.view.View;
  
 import com.mopub.nativeads.CustomEventNative;
 import com.mopub.nativeads.NativeErrorCode;
 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.TWMNativeAd;
 
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import java.util.Map;
 
 // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubNative 填入後台的Custom Event Class欄位
 public class TAMediaMopubNative extends CustomEventNative {
     private static final String TAG = "TAMediaMopubNative";
 
     private TWMNativeAd nativeAd;
 
     @Override
     protected void loadNativeAd(final Context context,
                                 final CustomEventNativeListener customEventNativeListener,
                                 final Map<String, Object> localExtras,
                                 final Map<String, String> serverExtras) {
         Log.d(TAG, "loadNativeAd(" + serverExtras.get("adUnitId") + ") invoked!!");
         String adUnitId = serverExtras.get("adUnitId"); //後台設定的adUnitId會透過此Key值取得
         nativeAd = new TWMNativeAd((Activity) context, adUnitId);
         final TWMediaStaticNativeAd staticNativeAd = new TWMediaStaticNativeAd(nativeAd, customEventNativeListener);
         nativeAd.setAdListener(staticNativeAd);
 
         //String testId = Utility.getEncodedAndroidId(context);
         nativeAd.loadAd(new TWMAdRequest());
     }
 
     static class TWMediaStaticNativeAd extends com.mopub.nativeads.StaticNativeAd implements TWMAdViewListener {
 
         private final CustomEventNativeListener mCustomEventNativeListener;
         private final TWMNativeAd nativeAd;
 
         // Native ad assets.
         private String longSubject;
         private String body;
         private String iconUrl;
         private String imageUrl;
         private String targetUrl;
         private String callToAction;
 
         public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) {
             mCustomEventNativeListener = customEventNativeListener;
             nativeAd = nativeAD;
         }
 
         @Override
         public void prepare(final View view) {
             super.prepare(view);
 
             // Set click event to all view.
             view.setOnClickListener(clickListener);
         }
 
         @Override
         public void clear(View view) {
             view.setOnClickListener(null);
             super.clear(view);
         }
 
         @Override
         public void destroy() {
             nativeAd.destroy();
             super.destroy();
         }
 
         // TWMAdViewListener
         @Override
         public void onReceiveAd(TWMAd twmAd) {
             if (nativeAd.isReady()) {
                 JSONObject content = nativeAd.getNativeAdContent();
                 try {
                     longSubject = (content.has("LONGSUBJECT")) ? content.getString("LONGSUBJECT") : null;
                     body = (content.has("BODY")) ? content.getString("BODY") : null;
                     iconUrl = (content.has("ICONSQUARE")) ? content.getString("ICONSQUARE") : null;
                     imageUrl = (content.has("IMAGE1200X627")) ? content.getString("IMAGE1200X627") : null;
                     targetUrl = (content.has("nurl")) ? content.getString("nurl") : null;
                     callToAction = (content.has("CTA")) ? content.getString("CTA") : null;
                     if(longSubject != null) setTitle(longSubject);
                     if(body != null) setText(body);
                     if(imageUrl != null) setMainImageUrl(imageUrl);
                     if(iconUrl != null) setIconImageUrl(iconUrl);
                     if(targetUrl != null) setClickDestinationUrl(targetUrl);
                     if(callToAction != null) setCallToAction(callToAction);
 
                     if(mCustomEventNativeListener != null)
                         mCustomEventNativeListener.onNativeAdLoaded(this);
                 } catch (JSONException e) {
                     Log.e(TAG, "Native Content Parse Error: " + e.getMessage());
                     if(mCustomEventNativeListener != null)
                         mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_INVALID_STATE);
                 }
             }
         }
         @Override
         public void onFailedToReceiveAd(TWMAd twmAd, TWMAdRequest.ErrorCode errorCode) {
             mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.NETWORK_NO_FILL);
         }
         @Override
         public void onPresentScreen(TWMAd twmAd) {}
         @Override
         public void onDismissScreen(TWMAd twmAd) {}
         @Override
         public void onLeaveApplication(TWMAd twmAd) {}
 
         private View.OnClickListener clickListener = new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 notifyAdClicked();
                 nativeAd.handleClick();
             }
         };
 
     }
 }



回MoPub教學