「Android Integeration with New AdMob Mediation 2.0」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
(取消由Wikiuser對話)所作出的修訂 510)
 
(未顯示同一使用者於中間所作的 1 次修訂)
行 1: 行 1:
Mopub網站: https://app.mopub.com, 登入可對後台進行設定。<br>
*新版AdMob Mediation整合
: 請將擋廣告的插件關閉(如Adblock),否則後台瀏覽、設定會有問題<br><br>
新版AdMob網站, https://apps.admob.com, 登入後點選 "<b>營利</b>",
並點選將要新增"<b>廣告中介服務</b>"的應用程式<br/> [[File:android_22.png]]  ,<br/>
點選"<b>編輯中介服務</b>", 出現中介服務編輯畫面,選擇"<b>新增廣告聯播網</b>"<br/> [[檔案:android_23.png]]<br/>
再點選"<b>+自訂事件</b>"<br/>[[檔案:android_24.png]],<br/>  出現自訂事件設定畫面 <br/>[[檔案:android_25.png]],<br/>
依序填入
#Class Name : 請填入實作(implements <b>com.google.ads.mediation.customevent.CustomEventBanner</b>)的類別名稱(含完整的package name, 如<b>com.taiwanmobile.pt.adp.mediation.TAMediaBanner</b>)
# Label : TAMedia (任意自定的名稱)
# Parameter : 請填入您的TAMedia Slot Id (可向您的TAMedia窗口詢問),<br/>
完成後,按下左下角的"儲存",將自訂事件的內容儲存起來, 並回到應用程式管理畫面,[[檔案:android_26.png]]<br/>再按一次"儲存"將廣告中介服務的設定進行儲存


== Mopub後台設定 ==
::*整合至應用程式
<big><u>Step 1: 新增APP與版位,取得Mopub版位ID</u></big><br>
:::*Banner,依照下述方式將AdMob所提供的CustomEventBanner進行實作
進入Apps分頁後,點擊Add a New App<br>
[[image:Mopub_Step1-1.png | 700px]]<br><br>
 
輸入APP資訊,如APP Name、Package Name…等<br>
[[image:Mopub_Step1-2.png | 700px]]<br><br>
 
新增版位並進行設定<br>
:a. Format為橫幅廣告Banner (320x50):<br>
[[image:Mopub_Step1-3.png | 700px]]<br><br>
 
:b. Format為插頁式廣告Fullscreen (320x480):<br>
[[image:Mopub_Step1-4.png | 700px]]<br><br>
 
:c. Format為原生廣告Native (Custom Layout):<br>
[[image:Mopub_Step1-5.png | 700px]]<br><br>
 
完成設定後,點選Save and View Code Integration,並記下Mopub的版位ID<br>
[[image:Mopub_Step1-6.png | 700px]]<br><br>
[[image:Mopub_Step1-7.png | 700px]]<br><br>
 
若欲新增其他版位,請點選剛剛新增的APP並點選Add an Ad Unit<br>
[[image:Mopub_Step1-8.png | 700px]]<br><br>
[[image:Mopub_Step1-9.png | 700px]]<br><br>
<br>
<br>
 
<big><u>Step 2: 新增廣告聯播網</u></big><br>
進入Network分頁,並點選Add a Network<br>
[[image:Mopub_Step2-1.png | 700px]]<br><br>
 
點選Custom Native Network<br>
[[image:Mopub_Step2-2.png | 700px]]<br><br>
 
設定Network Title(名字可任意取),並設定實作的類別名稱與參數,以下以Banner作為範例:<br>
[[image:Mopub_Step2-3.png | 700px]]<br><br>
 
依欄位填入<br>
<ol>
<li>Custom Event Class: 用於實作com.mopub.mobileads.CustomEventBanner的類別名稱,必須輸入完整的package name,如com.taiwanmobile.pt.adp.mediation.TAMediaMopubBanner</li>
<li>Custom Event Class Data: 請依指定格式輸入您的版位ID (可向您的TAMedia窗口詢問),格式為 {"adUnitId":"<版位ID>"}</li>
</ol>
輸入完畢後,後台會自動儲存<br>
[[image:Mopub_Step2-4.png | 700px]]<br><br>
 
最後進入Segment分頁,啟用方才設定的聯播網<br>
[[image:Mopub_Step2-5.png | 700px]]<br><br>
 
若有多個聯播網,可透過eCPM調整不同聯播網的優先權<br>
[[image:Mopub_Step2-6.png | 700px]]<br><br>
<br>
<br>
 
== Mopub程式整合 ==
<big><u>Step 1: Download TAMedia & Mopub SDK</u></big><br>
:a. 請參照 [https://github.com/mopub/mopub-android-sdk/wiki/Getting-Started#requirements-and-dependencies Mopub Github] 所提供的方式,將Mopub SDK加到您的專案中。<br>
:b. [[Download Android SDK 2 | Download TAMedia SDK]]<br><br>
 
<big><u>Step 2: 在AndroidManifest.xml加入所需的權限與Activity</u></big><br>
  &lt;!-- Mopub & TAMedia Permission --&gt;
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> &lt;!-- optional --&gt;
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> &lt;!-- optional --&gt;<br>
  &lt;!-- TAMedia Permission --&gt;
  <uses-permission android:name="android.permission.READ_PHONE_STATE" /> &lt;!-- SDK4.0以上版本請移除 --&gt;
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.VIBRATE" /> &lt;!-- optional --&gt;
  <uses-permission android:name="android.permission.CAMERA"/>  &lt;!-- optional, SDK4.0上請加入, 以獲得更豐富的廣告內容 --&gt;
  <uses-permission android:name="android.permission.RECORD_AUDIO"/>  &lt;!-- optional, SDK4.0上請加入, 以獲得更豐富的廣告內容 --&gt;<br>
  &lt;!-- Mopub Permission --&gt;
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
  &lt;!-- TAMedia Activities --&gt;
  <activity
  android:name="com.taiwanmobile.pt.adp.view.TWMAdActivity"
  android:configChanges="orientation|keyboardHidden|navigation|keyboard|screenLayout|uiMode|screenSize|smallestScreenSize"
  android:hardwareAccelerated="true"
  android:theme="@android:style/Theme.Translucent" >
  </activity>
 
  &lt;!-- Mopub activity --&gt;
  <activity android:name="com.mopub.mobileads.MoPubActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.MraidActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.common.MoPubBrowser" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.mopub.mobileads.RewardedMraidActivity" android:configChanges="keyboardHidden|orientation|screenSize" />
  <activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity" android:configChanges="keyboardHidden|orientation|screenSize"/>
  <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
 
  &lt;!-- Mopub meta-data  --&gt;
  <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
 
<br>
<big><u>Step 3: 將code整合至專案中</u></big><br>
Banner: 依下列方式實作Mopub所提供的CustomEventBanner<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.util.Log;
    
    
   import com.mopub.common.util.Views;
   import com.google.ads.AdRequest;
   import com.mopub.mobileads.CustomEventBanner;
  import com.google.ads.AdSize;
   import com.mopub.mobileads.MoPubErrorCode;
  import com.google.ads.mediation.MediationAdRequest;
   import com.google.ads.mediation.customevent.CustomEventBanner;
   import com.google.ads.mediation.customevent.CustomEventBannerListener;
   import com.taiwanmobile.pt.adp.view.TWMAd;
   import com.taiwanmobile.pt.adp.view.TWMAd;
   import com.taiwanmobile.pt.adp.view.TWMAdRequest;
   import com.taiwanmobile.pt.adp.view.TWMAdRequest;
  import com.taiwanmobile.pt.adp.view.TWMAdRequest.ErrorCode;
   import com.taiwanmobile.pt.adp.view.TWMAdSize;
   import com.taiwanmobile.pt.adp.view.TWMAdSize;
   import com.taiwanmobile.pt.adp.view.TWMAdView;
   import com.taiwanmobile.pt.adp.view.TWMAdView;
   import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
   import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
  import com.taiwanmobile.pt.util.Log;
  // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述
  public class TAMediaBanner implements CustomEventBanner {
 
  private static final String TAG = "TAMediaBanner";
    
    
  import java.util.Map;
  @Override
  public void destroy() {
 
  if(adView != null){
  adView.destroy();
  }
 
  }
 
  //將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
  private TWMAdRequest convertAdRequest(MediationAdRequest request) {
  TWMAdRequest adRequest = new TWMAdRequest();
  if (request.getBirthday() != null) {
  adRequest.setBirthday(request.getBirthday());
  }
  if (request.getAgeInYears() != null) {
  adRequest.setAge(request.getAgeInYears());
  }
    
    
  // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubBanner 填入後台的Custom Event Class欄位
  if (request.getGender() != null) {
  public class TAMediaMopubBanner extends CustomEventBanner {
  if (request.getGender().equals(AdRequest.Gender.FEMALE)) {
      private static final String TAG = "TAMediaMopubBanner";
  adRequest.setGender(TWMAdRequest.Gender.FEMALE);
  } else if (request.getGender().equals(AdRequest.Gender.MALE)) {
  adRequest.setGender(TWMAdRequest.Gender.MALE);
  } else {
  adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
  }
  }
    
    
      private TWMAdView adView = null;
  return adRequest;
  }
 
  //將AdMob的AdSize轉換為TWMAdSize
  private TWMAdSize convertAdSize(AdSize adSize) {
  if(adSize.equals(AdSize.BANNER)){
  return TWMAdSize.BANNER;
  }else if(adSize.equals(AdSize.IAB_BANNER)){
  return TWMAdSize.IAB_BANNER;
  }else if(adSize.equals(AdSize.IAB_MRECT)){
  return TWMAdSize.IAB_MRECT;
  }else if(adSize.equals(AdSize.IAB_LEADERBOARD)){
  return TWMAdSize.IAB_LEADERBOARD;
  }else if(adSize.equals(AdSize.SMART_BANNER)){
          return TWMAdSize.SMART_BANNER;
  }else{
                        return TWMAdSize.BANNER;
  }
  }
 
 
  private TWMAdView adView = null;
    
    
      @Override
  @Override
      protected void loadBanner(Context context, final CustomEventBannerListener customEventBannerListener, Map<String, Object> localExtras, Map<String, String> serverExtras) {
  public void requestBannerAd(final CustomEventBannerListener customListener, Activity activity,
          Log.d(TAG, "loadBanner(" + serverExtras.get("adUnitId") + ") invoked!!");
  String label, String serverParameter, AdSize adSize, MediationAdRequest request,
  Object extras) {
  Log.d(TAG, "requestBannerAd("+label+"/"+serverParameter+") invoked!!");
 
  adView = new TWMAdView(activity,convertAdSize(adSize),label);
 
  adView.setAdListener(new TWMAdViewListener(){
    
    
          String adUnitId = serverExtras.get("adUnitId");
  @Override
          adView = new TWMAdView( (Activity)context, TWMAdSize.SMART_BANNER, adUnitId );
  public void onReceiveAd(TWMAd ad) {
          adView.setAdListener(new TWMAdViewListener(){
  customListener.onReceivedAd(adView);
              @Override
 
              public void onReceiveAd(TWMAd ad) {
  }
                  customEventBannerListener.onBannerLoaded(adView);
              }
    
    
              @Override
  @Override
              public void onFailedToReceiveAd(TWMAd ad, TWMAdRequest.ErrorCode errorCode) {
  public void onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
                  customEventBannerListener.onBannerFailed(MoPubErrorCode.NETWORK_NO_FILL);
  customListener.onFailedToReceiveAd();
              }
 
  }
    
    
              @Override
  @Override
              public void onPresentScreen(TWMAd ad) {
  public void onPresentScreen(TWMAd ad) {
                  customEventBannerListener.onBannerClicked();
  customListener.onPresentScreen();
              }
                                //若您需要在AdMob報表上觀察點擊率, 請加入CustomEventBannerListener.onClick()
                                customListener.onClick();
 
  }
    
    
              @Override
  @Override
              public void onDismissScreen(TWMAd ad) {}
  public void onDismissScreen(TWMAd ad) {
  customListener.onDismissScreen();
 
  }
    
    
              @Override
  @Override
              public void onLeaveApplication(TWMAd ad) {
  public void onLeaveApplication(TWMAd ad) {
                  customEventBannerListener.onLeaveApplication();
  customListener.onLeaveApplication();
              }
 
  }
 
  });
 
  adView.loadAd(convertAdRequest(request));
 
 
  }
    
    
          });
          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.destroy();
          }
      }
   }
   }
<br>
:::*Interstitial,依照下述方式將AdMob所提供的CustomEventInterstitial進行實作
Interstitial: 依下列方式實作Mopub所提供的CustomEventInterstitial<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.util.Log;
    
    
   import com.mopub.mobileads.CustomEventInterstitial;
   import com.google.ads.AdRequest;
   import com.mopub.mobileads.MoPubErrorCode;
  import com.google.ads.mediation.MediationAdRequest;
  import com.google.ads.mediation.customevent.CustomEventInterstitial;
   import com.google.ads.mediation.customevent.CustomEventInterstitialListener;
   import com.taiwanmobile.pt.adp.view.TWMAd;
   import com.taiwanmobile.pt.adp.view.TWMAd;
   import com.taiwanmobile.pt.adp.view.TWMAdRequest;
   import com.taiwanmobile.pt.adp.view.TWMAdRequest;
  import com.taiwanmobile.pt.adp.view.TWMAdRequest.ErrorCode;
   import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
   import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
   import com.taiwanmobile.pt.adp.view.TWMInterstitialAd;
   import com.taiwanmobile.pt.adp.view.TWMInterstitialAd;
  // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述
  public class TAMediaInterstitial implements CustomEventInterstitial {
    
    
  import java.util.Map;
  // 將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
  private TWMAdRequest convertAdRequest(MediationAdRequest request) {
  TWMAdRequest adRequest = new TWMAdRequest();
  if (request.getBirthday() != null) {
  adRequest.setBirthday(request.getBirthday());
  }
  if (request.getAgeInYears() != null) {
  adRequest.setAge(request.getAgeInYears());
  }
    
    
  // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaMopubInterstitial 填入後台的Custom Event Class欄位
  if (request.getGender() != null) {
  public class TAMediaMopubInterstitial extends CustomEventInterstitial {
  if (request.getGender().equals(AdRequest.Gender.FEMALE)) {
      private static final String TAG = "TAMediaInterstitial";
  adRequest.setGender(TWMAdRequest.Gender.FEMALE);
  } else if (request.getGender().equals(AdRequest.Gender.MALE)) {
  adRequest.setGender(TWMAdRequest.Gender.MALE);
  } else {
  adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
  }
  }
    
    
      private TWMInterstitialAd interstitialAd  = null;
  return adRequest;
      private CustomEventInterstitialListener mInterstitialListener;
  }
    
    
      @Override
  @Override
      protected void loadInterstitial(final Context context,
  public void destroy() {
                                      final CustomEventInterstitialListener customEventInterstitialListener,
  // TODO Auto-generated method stub
                                      final Map<String, Object> localExtras,
                                      final Map<String, String> serverExtras) {
          Log.d(TAG, "loadInterstitial(" + serverExtras.get("adUnitId") + ") invoked!!");
    
    
          mInterstitialListener = customEventInterstitialListener;
  }
    
    
          String adUnitId = serverExtras.get("adUnitId");
  private TWMInterstitialAd interstitialAd = null;
          interstitialAd = new TWMInterstitialAd( (Activity)context, adUnitId );
          interstitialAd.setAdListener(new TWMAdViewListener(){
              @Override
              public void onReceiveAd(TWMAd ad) {
                  mInterstitialListener.onInterstitialLoaded();
              }
    
    
              @Override
  @Override
              public void onFailedToReceiveAd(TWMAd ad, TWMAdRequest.ErrorCode errorCode) {
  public void requestInterstitialAd(
                  mInterstitialListener.onInterstitialFailed(MoPubErrorCode.NETWORK_NO_FILL);
  final CustomEventInterstitialListener customListener,
              }
  Activity activity, String label, String serverParameter,
  MediationAdRequest request, Object extra) {
  interstitialAd = new TWMInterstitialAd(activity, serverParameter);
    
    
              @Override
  interstitialAd.setAdListener(new TWMAdViewListener() {
              public void onPresentScreen(TWMAd ad) {
                  mInterstitialListener.onInterstitialClicked();
              }
    
    
              @Override
  @Override
              public void onDismissScreen(TWMAd ad) {
  public void onReceiveAd(TWMAd ad) {
                  mInterstitialListener.onInterstitialDismissed();
  customListener.onReceivedAd();
              }
    
    
              @Override
  }
              public void onLeaveApplication(TWMAd ad) {
                  mInterstitialListener.onLeaveApplication();
              }
    
    
          });
  @Override
          interstitialAd.loadAd(new TWMAdRequest());
  public void onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
  customListener.onFailedToReceiveAd();
    
    
      }
  }
    
    
      @Override
  @Override
      protected void showInterstitial() {
  public void onPresentScreen(TWMAd ad) {
          // Called when MoPubInterstitial Object called show()
  customListener.onPresentScreen();
          if (interstitialAd != null) {
              interstitialAd.show();
              mInterstitialListener.onInterstitialShown();
          }
      }
    
    
      @Override
  }
      protected void onInvalidate() {
          // Called when MoPubInterstitial is being invalidated or destroyed.
      }
    
    
  }
  @Override
<br>
  public void onDismissScreen(TWMAd ad) {
Native: 依下列方式實作Mopub所提供的CustomEventNative<br>
  customListener.onDismissScreen();
  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;
  @Override
  import com.mopub.nativeads.NativeErrorCode;
  public void onLeaveApplication(TWMAd ad) {
  import com.taiwanmobile.pt.adp.view.TWMAd;
  customListener.onLeaveApplication();
  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欄位
  interstitialAd.loadAd(convertAdRequest(request));
  public class TAMediaMopubNative extends CustomEventNative {
      private static final String TAG = "TAMediaMopubNative";
    
    
      private TWMNativeAd nativeAd;
  }
    
    
      @Override
  @Override
      protected void loadNativeAd(final Context context,
  public void showInterstitial() {
                                  final CustomEventNativeListener customEventNativeListener,
  if (interstitialAd != null) {
                                  final Map<String, Object> localExtras,
  interstitialAd.show();
                                  final Map<String, String> serverExtras) {
  }
          Log.d(TAG, "loadNativeAd(" + serverExtras.get("adUnitId") + ") invoked!!");
    
    
          String adUnitId = serverExtras.get("adUnitId");
  }
          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) {}
      }
    
    
   }
   }
<br>
 
加入上述Code後,即完成Mopub整合,開發者可透過Mopub取得TAMedia的廣告。<br>
::[[Download_Android_SDK_2|Download Android SDK and Sample]]
<br>
::加入必要的權限<br/>TWMAd服務需使用”存取網路狀態”、”讀取約略位置”、”網路”等權限,請在AndroidManifest.xml檔案當中進行下列權限的宣告。
取得廣告的方式請見Tutorial Project。
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> &lt;!-- optional --&gt;
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> &lt;!-- optional --&gt;
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIRBATE" /> &lt;!-- optional --&gt;
::加入必要Activity
&lt;!-- TAMedia Activities --&gt;
<activity
  android:name="com.taiwanmobile.pt.adp.view.TWMAdActivity"         
  android:configChanges="orientation|keyboardHidden|navigation|keyboard|screenLayout|uiMode|screenSize|smallestScreenSize"
  android:hardwareAccelerated="true"
  android:theme="@android:style/Theme.Translucent" >
</activity>
</activity>
&lt;!-- Google Play Service Activities --&gt;
<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
<activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
::在layout檔案中加入可放置廣告的LinearLayout(※僅供參考,開發者可依照自訂的layout進行配置使用)
<LinearLayout
android:id="@+id/Layout_AdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
::在程式加入Ad的宣告
  public class MainActivity extends Activity {
  LinearLayout layout = null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    layout = (LinearLayout) this.findViewById(R.id.Layout_AdView);
    adView = new AdView(this);
    adView.setAdUnitId("<Mediation ID>");
    adView.setAdSize(AdSize.BANNER);   
    layout.addView(adView);
    AdRequest request = new AdRequest.Builder().build();
    adView.loadAd(request);
  }
::*聯播網投放與維護
::當APP設定好Mediation並且發佈出去後,可直接透過Mediation網頁進行版位管理與聯播網比率調整,此設定會在1小時後生效。

於 2017年5月17日 (三) 09:25 的最新修訂

  • 新版AdMob Mediation整合

新版AdMob網站, https://apps.admob.com, 登入後點選 "營利", 並點選將要新增"廣告中介服務"的應用程式
Android 22.png ,
點選"編輯中介服務", 出現中介服務編輯畫面,選擇"新增廣告聯播網"
Android 23.png
再點選"+自訂事件"
Android 24.png,
出現自訂事件設定畫面
Android 25.png,
依序填入

  1. Class Name : 請填入實作(implements com.google.ads.mediation.customevent.CustomEventBanner)的類別名稱(含完整的package name, 如com.taiwanmobile.pt.adp.mediation.TAMediaBanner)
  2. Label : TAMedia (任意自定的名稱)
  3. Parameter : 請填入您的TAMedia Slot Id (可向您的TAMedia窗口詢問),

完成後,按下左下角的"儲存",將自訂事件的內容儲存起來, 並回到應用程式管理畫面,Android 26.png
再按一次"儲存"將廣告中介服務的設定進行儲存

  • 整合至應用程式
  • Banner,依照下述方式將AdMob所提供的CustomEventBanner進行實作
 package com.taiwanmobile.pt.adp.mediation;
 
 import android.app.Activity;
 
 import com.google.ads.AdRequest;
 import com.google.ads.AdSize;
 import com.google.ads.mediation.MediationAdRequest;
 import com.google.ads.mediation.customevent.CustomEventBanner;
 import com.google.ads.mediation.customevent.CustomEventBannerListener;
 import com.taiwanmobile.pt.adp.view.TWMAd;
 import com.taiwanmobile.pt.adp.view.TWMAdRequest;
 import com.taiwanmobile.pt.adp.view.TWMAdRequest.ErrorCode;
 import com.taiwanmobile.pt.adp.view.TWMAdSize;
 import com.taiwanmobile.pt.adp.view.TWMAdView;
 import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
 import com.taiwanmobile.pt.util.Log;
 // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述
 public class TAMediaBanner implements CustomEventBanner {
 	
 	private static final String TAG = "TAMediaBanner";
 
 	@Override
 	public void destroy() {
 		
 		if(adView != null){
 			adView.destroy();
 		}
 		
 	}
 	
 	//將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
 	private TWMAdRequest convertAdRequest(MediationAdRequest request) {
 		TWMAdRequest adRequest = new TWMAdRequest();
 		if (request.getBirthday() != null) {
 			adRequest.setBirthday(request.getBirthday());
 		}
 		if (request.getAgeInYears() != null) {
 			adRequest.setAge(request.getAgeInYears());
 		}
 
 		if (request.getGender() != null) {
 			if (request.getGender().equals(AdRequest.Gender.FEMALE)) {
 				adRequest.setGender(TWMAdRequest.Gender.FEMALE);
 			} else if (request.getGender().equals(AdRequest.Gender.MALE)) {
 				adRequest.setGender(TWMAdRequest.Gender.MALE);
 			} else {
 				adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
 			}
 		}
 
 		return adRequest;
 	}
 	
 	//將AdMob的AdSize轉換為TWMAdSize
 	private TWMAdSize convertAdSize(AdSize adSize) {
 		if(adSize.equals(AdSize.BANNER)){
 			return TWMAdSize.BANNER;
 		}else if(adSize.equals(AdSize.IAB_BANNER)){
 			return TWMAdSize.IAB_BANNER;
 		}else if(adSize.equals(AdSize.IAB_MRECT)){
 			return TWMAdSize.IAB_MRECT;
 		}else if(adSize.equals(AdSize.IAB_LEADERBOARD)){
 			return TWMAdSize.IAB_LEADERBOARD;
 		}else if(adSize.equals(AdSize.SMART_BANNER)){
 		        return TWMAdSize.SMART_BANNER;
 		}else{
                       return TWMAdSize.BANNER;
 		}
 	}
 	
 	
 	private TWMAdView adView = null;
 
 	@Override
 	public void requestBannerAd(final CustomEventBannerListener customListener, Activity activity,
 			String label, String serverParameter, AdSize adSize, MediationAdRequest request,
 			Object extras) {
 		Log.d(TAG, "requestBannerAd("+label+"/"+serverParameter+") invoked!!");
 		
 		adView = new TWMAdView(activity,convertAdSize(adSize),label);
 		
 		adView.setAdListener(new TWMAdViewListener(){
 
 			@Override
 			public void onReceiveAd(TWMAd ad) {
 				customListener.onReceivedAd(adView);
 				
 			}
 
 			@Override
 			public void onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
 				customListener.onFailedToReceiveAd();
 				
 			}
 
 			@Override
 			public void onPresentScreen(TWMAd ad) {
 				customListener.onPresentScreen();
                               //若您需要在AdMob報表上觀察點擊率, 請加入CustomEventBannerListener.onClick()
                               customListener.onClick();
 				
 			}
 
 			@Override
 			public void onDismissScreen(TWMAd ad) {
 				customListener.onDismissScreen();
 				
 			}
 
 			@Override
 			public void onLeaveApplication(TWMAd ad) {
 				customListener.onLeaveApplication();
 				
 			}
 			
 		});
 		
 		adView.loadAd(convertAdRequest(request));
 		
 		
 	}
 
 }
  • Interstitial,依照下述方式將AdMob所提供的CustomEventInterstitial進行實作
 package com.taiwanmobile.pt.adp.mediation;
 
 import android.app.Activity;
 
 import com.google.ads.AdRequest;
 import com.google.ads.mediation.MediationAdRequest;
 import com.google.ads.mediation.customevent.CustomEventInterstitial;
 import com.google.ads.mediation.customevent.CustomEventInterstitialListener;
 import com.taiwanmobile.pt.adp.view.TWMAd;
 import com.taiwanmobile.pt.adp.view.TWMAdRequest;
 import com.taiwanmobile.pt.adp.view.TWMAdRequest.ErrorCode;
 import com.taiwanmobile.pt.adp.view.TWMAdViewListener;
 import com.taiwanmobile.pt.adp.view.TWMInterstitialAd;
 // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述
 public class TAMediaInterstitial implements CustomEventInterstitial {
 
 	// 將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
 	private TWMAdRequest convertAdRequest(MediationAdRequest request) {
 		TWMAdRequest adRequest = new TWMAdRequest();
 		if (request.getBirthday() != null) {
 			adRequest.setBirthday(request.getBirthday());
 		}
 		if (request.getAgeInYears() != null) {
 			adRequest.setAge(request.getAgeInYears());
 		}
 
 		if (request.getGender() != null) {
 			if (request.getGender().equals(AdRequest.Gender.FEMALE)) {
 				adRequest.setGender(TWMAdRequest.Gender.FEMALE);
 			} else if (request.getGender().equals(AdRequest.Gender.MALE)) {
 				adRequest.setGender(TWMAdRequest.Gender.MALE);
 			} else {
 				adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
 			}
 		}
 
 		return adRequest;
 	}
 
 	@Override
 	public void destroy() {
 		// TODO Auto-generated method stub
 
 	}
 
 	private TWMInterstitialAd interstitialAd = null;
 
 	@Override
 	public void requestInterstitialAd(
 			final CustomEventInterstitialListener customListener,
 			Activity activity, String label, String serverParameter,
 			MediationAdRequest request, Object extra) {
 		interstitialAd = new TWMInterstitialAd(activity, serverParameter);
 
 		interstitialAd.setAdListener(new TWMAdViewListener() {
 
 			@Override
 			public void onReceiveAd(TWMAd ad) {
 				customListener.onReceivedAd();
 
 			}
 
 			@Override
 			public void onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
 				customListener.onFailedToReceiveAd();
 
 			}
 
 			@Override
 			public void onPresentScreen(TWMAd ad) {
 				customListener.onPresentScreen();
 
 			}
 
 			@Override
 			public void onDismissScreen(TWMAd ad) {
 				customListener.onDismissScreen();
 
 			}
 
 			@Override
 			public void onLeaveApplication(TWMAd ad) {
 				customListener.onLeaveApplication();
 
 			}
 
 		});
 
 		interstitialAd.loadAd(convertAdRequest(request));
 
 	}
 
 	@Override
 	public void showInterstitial() {
 		if (interstitialAd != null) {
 			interstitialAd.show();
 		}
 
 	}
 
 }
Download Android SDK and Sample
加入必要的權限
TWMAd服務需使用”存取網路狀態”、”讀取約略位置”、”網路”等權限,請在AndroidManifest.xml檔案當中進行下列權限的宣告。
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- optional -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- optional -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.VIRBATE" /> <!-- optional -->
加入必要Activity
<!-- TAMedia Activities -->
<activity
 android:name="com.taiwanmobile.pt.adp.view.TWMAdActivity"           
 android:configChanges="orientation|keyboardHidden|navigation|keyboard|screenLayout|uiMode|screenSize|smallestScreenSize"
 android:hardwareAccelerated="true"
 android:theme="@android:style/Theme.Translucent" >
</activity>
</activity>
<!-- Google Play Service Activities -->
<meta-data
           android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />
<activity
           android:name="com.google.android.gms.ads.AdActivity"
           android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
在layout檔案中加入可放置廣告的LinearLayout(※僅供參考,開發者可依照自訂的layout進行配置使用)
<LinearLayout
android:id="@+id/Layout_AdView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在程式加入Ad的宣告
 public class MainActivity extends Activity {
  LinearLayout layout = null;
  @Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   layout = (LinearLayout) this.findViewById(R.id.Layout_AdView);
   adView = new AdView(this);
   adView.setAdUnitId("<Mediation ID>");
   adView.setAdSize(AdSize.BANNER);    
   layout.addView(adView);
   AdRequest request = new AdRequest.Builder().build();
   adView.loadAd(request);
  }
  • 聯播網投放與維護
當APP設定好Mediation並且發佈出去後,可直接透過Mediation網頁進行版位管理與聯播網比率調整,此設定會在1小時後生效。