「Android Mopub Native」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
(新頁面: 請建立一個Class並繼承Mopub所提供的CustomEventNative<br><br> Class實作內容如下:<br> package com.taiwanmobile.pt.adp.mediation; import android.app.Activity; ...)
 
imported>Wikiuser
行 20: 行 20:
   import java.util.Map;
   import java.util.Map;
    
    
   // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubNative 填入後台的Custom Event Class欄位
   '''//需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubNative 填入後台的Custom Event Class欄位'''
   public class TAMediaMopubNative extends CustomEventNative {
   public class TAMediaMopubNative extends CustomEventNative {
       private static final String TAG = "TAMediaMopubNative";
       private static final String TAG = "TAMediaMopubNative";
行 33: 行 33:
           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);

於 2017年7月10日 (一) 09:21 的修訂

請建立一個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);
         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;
 
         public TWMediaStaticNativeAd(final TWMNativeAd nativeAD, final CustomEventNativeListener customEventNativeListener) {
             mCustomEventNativeListener = customEventNativeListener;
             nativeAd = nativeAD;
         }
 
         @Override
         public void destroy() {
             nativeAd.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();
                 }
             });
             notifyAdImpressed();
         }
 
         // 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;
                     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);
 
                     mCustomEventNativeListener.onNativeAdLoaded(this);
                 } catch (JSONException e) {
                     Log.e(TAG, "Native Content Parse Error: " + e.getMessage());
                     mCustomEventNativeListener.onNativeAdFailed(NativeErrorCode.INVALID_RESPONSE);
                 }
             }
         }
         @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) {}
     }
 
 }



回MoPub教學