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

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
行 1: 行 1:
AdMob網站: https://apps.admob.com/ <br><br>
<p style="font-size: 18px;">AdMob網站: https://apps.admob.com/ <br><br></p>


= '''新增應用程式''' =
= '''新增應用程式''' =
行 8: 行 8:


<li><p style="font-size: 18px;">輸入應用程式名稱並選擇平台</p></li>
<li><p style="font-size: 18px;">輸入應用程式名稱並選擇平台</p></li>
[[ image:admob_application_01.PNG | 600px ]]
[[ image:admob_application_01.PNG | 800px ]]
<br><br><br>
<br><br><br>


<li><p style="font-size: 18px;">建立完成,請記下'''應用程式 ID'''</p></li>
<li><p style="font-size: 18px;">建立完成,請記下'''應用程式 ID'''</p></li>
[[ image:admob_application_02.PNG | 600px ]]
[[ image:admob_application_02.PNG | 800px ]]
</ul>
</ul>
<br><br><br>
<br><br><br>
行 23: 行 23:
<br><br><br>
<br><br><br>


<li><p style="font-size: 18px;">選擇欲建立的廣告單元 (以下以'''橫幅廣告'''為例)</p></li>
<li><p style="font-size: 18px;">選擇欲建立的廣告單元 ('''橫幅廣告'''為例)</p></li>
[[ image:admob_adunit_01.PNG | 600px ]]
[[ image:admob_adunit_01.PNG | 800px ]]
<br><br><br>
<br><br><br>


行 31: 行 31:
<br><br><br>
<br><br><br>


<li><p style="font-size: 18px;">建立完成,請記下'''廣告單元ID'''</p></li>
<li><p style="font-size: 18px;">建立完成,請記下 AdMob 的'''廣告單元ID'''</p></li>
[[ image:admob_adunit_03.PNG | 600px ]]
[[ image:admob_adunit_03.PNG | 800px ]]
</ul>
</ul><br><br><br>
<br><br><br>




行 42: 行 41:
<ul>
<ul>
<li><p style="font-size: 18px;">進入 AdMob 網頁選擇 '''中介服務''' > '''新增中介服務群組'''</p></li>
<li><p style="font-size: 18px;">進入 AdMob 網頁選擇 '''中介服務''' > '''新增中介服務群組'''</p></li>
[[ image:admob_medgroup_00.PNG | 600px ]]
[[ image:admob_medgroup_00.PNG | 800px ]]
<br><br><br>
 
<li><p style="font-size: 18px;">設定廣告格式、平台</p></li>
[[ image:admob_medgroup_01.PNG | 800px ]]
<br><br><br>
 
<li><p style="font-size: 18px;">設定中介群組名稱並點選'''加入廣告單元'''</p></li>
[[ image:admob_medgroup_02.PNG | 800px ]]
<br><br><br>
 
<li><p style="font-size: 18px;">選擇先前建立的廣告單元 (以'''橫幅廣告'''為例)</p></li>
[[ image:admob_medgroup_03.PNG | 800px ]]
<br><br><br>
<br><br><br>


<li><p style="font-size: 18px;"></p></li>
<li>
[[ image:admob_medgroup_01.PNG | 600px ]]
  <p style="font-size: 18px;">點選'''新增自訂事件''',並設定下列相關資訊:</p>
  <p style="font-size: 16px">'''有效千次曝光出價''': 越高代表該家聯播網廣告曝光的機會越高</p>
  <p style="font-size: 16px">'''Class Name''': 根據您的專案輸入 mediation code 的 package name,如: com.taiwanmobile.pt.adp.mediation.TAMediaBanner (下方'''[[#實作 Mediation Code|實作 Mediation Code]]'''章節會說明Mediation Code的實作方式)</p>
  <p style="font-size: 16px">'''Parameter''': 輸入您的 TAMedia 廣告版位ID </p>
</li>
[[ image:admob_medgroup_04.PNG | 800px ]]
<br><br>
[[ image:admob_medgroup_05.PNG | 800px ]]
<br><br>
[[ image:admob_medgroup_06.PNG | 1200px ]]
<br><br><br>
<br><br><br>


<li><p style="font-size: 18px;"></p></li>
<li><p style="font-size: 18px;">點選完成後,中介服務群組的設置便結束</p></li>
[[ image:admob_medgroup_02.PNG | 600px ]]
[[ image:admob_medgroup_07.PNG | 1200px ]]
</ul>
</ul>
<br><br><br>
<br><br><br>
= '''程式整合''' =
== '''Getting Started''' ==
:<p style="font-size: 18px;">1. 請參考'''[https://developers.google.com/admob/android/quick-start?hl=zh-TW AdMob for Android]'''網站提供的方式整合Google Ads SDK,會使用到'''[[#新增應用程式|新增應用程式]]'''所取得的'''應用程式ID'''</p>
:<p style="font-size: 18px;">2. 下載'''[[Download MADP Android SDK | TAMedia Android SDK]]''',並參考'''[[Android Getting Started 3.0 | 開始使用Getting Started]]'''整合TAMedia Android SDK</p>
<br><br><br>
== '''實作 Mediation Code'''==
=== Banner ===
:<p style="font-size: 18px;">依照下列方式實作AdMob所提供的CustomEventBanner</p>
  package com.taiwanmobile.pt.adp.mediation;
 
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.util.Log;
 
  import com.google.android.gms.ads.AdRequest;
  import com.google.android.gms.ads.AdSize;
  import com.google.android.gms.ads.mediation.MediationAdRequest;
  import com.google.android.gms.ads.mediation.customevent.CustomEventBanner;
  import com.google.android.gms.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 java.util.Calendar;
  import java.util.Date;
 
  <b>// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於中介服務群組, 如[[ #新增中介服務群組 | 新增中介服務群組 ]]所述</b>
  public class TAMediaBanner implements CustomEventBanner {
     
      private static final String TAG = "TAMediaBanner";
     
      private TWMAdView adView = null;
 
      @Override
      public void requestBannerAd(final Context context,
                                  final CustomEventBannerListener customEventBannerListener,
                                  String serverParameter,
                                  AdSize adSize,
                                  MediationAdRequest mediationAdRequest,
                                  Bundle bundle) {
          <b>// serverParameter: AdMob 後台設定的 TAMedia Slot ID, 會藉由此參數獲得</b>
          Log.d(TAG, "requestBannerAd("+serverParameter+") invoked!!");
 
          adView = new TWMAdView( (Activity) context, convertAdSize(adSize), serverParameter);
          adView.setAdListener(new TWMAdViewListener(){
              @Override
              public void onReceiveAd(TWMAd ad) {
                  ((Activity) context).runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          customEventBannerListener.onAdLoaded(adView);
                      }
                  });
              }
 
              @Override
              public void onFailedToReceiveAd(TWMAd ad, final ErrorCode errorCode) {
                  ((Activity) context).runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          customEventBannerListener.onAdFailedToLoad(convertErrorCode(errorCode));
                      }
                  });
              }
 
              @Override
              public void onPresentScreen(TWMAd ad) {
                  ((Activity) context).runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          customEventBannerListener.onAdOpened();
                          <b>//若您需要在AdMob報表上觀察點擊率, 請加入CustomEventBannerListener.onClick()</b>
                          customEventBannerListener.onAdClicked();
                      }
                  });
 
              }
 
              @Override
              public void onDismissScreen(TWMAd ad) {
                  ((Activity) context).runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          customEventBannerListener.onAdClosed();
                      }
                  });
              }
 
              @Override
              public void onLeaveApplication(TWMAd ad) {
                  ((Activity) context).runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          customEventBannerListener.onAdLeftApplication();
                      }
                  });
              }
          });
 
          adView.loadAd(convertAdRequest(mediationAdRequest));
      }
 
      @Override
      public void onDestroy() {
          if(adView != null){
              adView.destroy();
          }
      }
 
      @Override
      public void onPause() {}
 
      @Override
      public void onResume() {}
 
 
      <b>//將AdMob所提供的MediationAdRequest轉換為TWMAdRequest</b>
      private TWMAdRequest convertAdRequest(MediationAdRequest request) {
          TWMAdRequest adRequest = new TWMAdRequest();
          if (request.getBirthday() != null) {
 
              Date bDay = request.getBirthday();
              adRequest.setBirthday(bDay);
 
              Calendar cal = Calendar.getInstance();
              cal.setTime(bDay);
              adRequest.setAge(getAgeByBirthday(
                      cal.get(Calendar.YEAR),
                      cal.get(Calendar.MONTH),
                      cal.get(Calendar.DATE)));
          }
 
          if (request.getGender() == AdRequest.GENDER_FEMALE) {
              adRequest.setGender(TWMAdRequest.Gender.FEMALE);
          } else if (request.getGender() == AdRequest.GENDER_MALE) {
              adRequest.setGender(TWMAdRequest.Gender.MALE);
          } else {
              adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
          }
 
          return adRequest;
      }
 
      <b>//將AdMob的AdSize轉換為TWMAdSize</b>
      private TWMAdSize convertAdSize(AdSize adSize) {
          if (isAdSizeEqual(adSize, TWMAdSize.BANNER)) {
              return TWMAdSize.BANNER;
          } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_MRECT)) {
              return TWMAdSize.IAB_MRECT;
          } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_BANNER)) {
              return TWMAdSize.IAB_BANNER;
          } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_LEADERBOARD)) {
              return TWMAdSize.IAB_LEADERBOARD;
          } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_WIDE_SKYSCRAPER)) {
              return TWMAdSize.IAB_WIDE_SKYSCRAPER;
          } else if (isAdSizeEqual(adSize, TWMAdSize.SMART_BANNER)) {
              return TWMAdSize.SMART_BANNER;
          } else {
              return TWMAdSize.BANNER;
          }
      }
 
      private boolean isAdSizeEqual(AdSize adSize, TWMAdSize twmAdSize) {
          return (adSize.getWidth() == twmAdSize.getWidth()
                  && adSize.getHeight() == twmAdSize.getHeight());
      }
 
      <b>//將TAMedia ErrorCode轉換為AdMob的ErrorCode</b>
      private int convertErrorCode(ErrorCode errorCode){
          if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
              return AdRequest.ERROR_CODE_INTERNAL_ERROR;
          }else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
              return AdRequest.ERROR_CODE_INVALID_REQUEST;
          }else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
              return AdRequest.ERROR_CODE_NETWORK_ERROR;
          }else if(errorCode.equals(ErrorCode.NO_FILL)){
              return AdRequest.ERROR_CODE_NO_FILL;
          }else
              return AdRequest.ERROR_CODE_NETWORK_ERROR;
      }
 
      private int getAgeByBirthday(int year, int month, int day){
          Calendar dob = Calendar.getInstance();
          Calendar today = Calendar.getInstance();
 
          dob.set(year, month, day);
 
          int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
          if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
              age--;
          }
 
          Integer ageInt = new Integer(age);
          return ageInt.intValue();
      }
 
  }
<br><br>
=== Interstitial ===
:<p style="font-size: 18px;">依照下列方式實作AdMob所提供的CustomEventInterstitial</p>
  package com.taiwanmobile.pt.adp.mediation;
 
  import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.util.Log;
 
 
  import com.google.android.gms.ads.AdRequest;
  import com.google.android.gms.ads.mediation.MediationAdRequest;
  import com.google.android.gms.ads.mediation.customevent.CustomEventInterstitial;
  import com.google.android.gms.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;
 
  import java.util.Calendar;
  import java.util.Date;
 
  <b>// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaInterstitial註冊於中介服務群組, 如[[ #新增中介服務群組 | 新增中介服務群組 ]]所述</b>
  public class TAMediaInterstitial implements CustomEventInterstitial {
 
  private TWMInterstitialAd interstitialAd = null;
 
  private static final String TAG ="TAMediaInterstitial";
 
  @Override
  public void requestInterstitialAd(final Context context,
  final CustomEventInterstitialListener customEventInterstitialListener,
  String serverParameter,
  MediationAdRequest mediationAdRequest,
  Bundle bundle) {
  <b>// serverParameter: AdMob 後台設定的 TAMedia Slot ID, 會藉由此參數獲得</b>
  Log.d(TAG, "requestInterstitialAd("+serverParameter+") ");
 
  interstitialAd = new TWMInterstitialAd((Activity)context, serverParameter);
 
  interstitialAd.setAdListener(new TWMAdViewListener() {
 
  @Override
  public void onReceiveAd(TWMAd ad) {
  ((Activity) context).runOnUiThread(new Runnable() {
  @Override
  public void run() {
  customEventInterstitialListener.onAdLoaded();
  }
  });
  }
 
  @Override
  public void onFailedToReceiveAd(TWMAd ad, final ErrorCode errorCode) {
  ((Activity) context).runOnUiThread(new Runnable() {
  @Override
  public void run() {
  customEventInterstitialListener.onAdFailedToLoad(convertErrorCode(errorCode));
  }
  });
  }
 
  @Override
  public void onPresentScreen(TWMAd ad) {
  ((Activity) context).runOnUiThread(new Runnable() {
  @Override
  public void run() {
  customEventInterstitialListener.onAdOpened();
  }
  });
  }
 
  @Override
  public void onDismissScreen(TWMAd ad) {
  ((Activity) context).runOnUiThread(new Runnable() {
  @Override
  public void run() {
  customEventInterstitialListener.onAdClosed();
  }
  });
  }
 
  @Override
  public void onLeaveApplication(TWMAd ad) {
  ((Activity) context).runOnUiThread(new Runnable() {
  @Override
  public void run() {
  customEventInterstitialListener.onAdLeftApplication();
  }
  });
  }
 
  });
 
  interstitialAd.loadAd(convertAdRequest(mediationAdRequest));
  }
 
  @Override
  public void showInterstitial() {
  if (interstitialAd != null) {
  interstitialAd.show();
  }
  }
 
  @Override
  public void onDestroy() {}
 
  @Override
  public void onPause() {}
 
  @Override
  public void onResume() {}
 
  <b>//將AdMob所提供的MediationAdRequest轉換為TWMAdRequest</b>
  private TWMAdRequest convertAdRequest(MediationAdRequest request) {
  TWMAdRequest adRequest = new TWMAdRequest();
  if (request.getBirthday() != null) {
 
  Date bDay = request.getBirthday();
  adRequest.setBirthday(bDay);
 
  Calendar cal = Calendar.getInstance();
  cal.setTime(bDay);
  adRequest.setAge(getAgeByBirthday(
  cal.get(Calendar.YEAR),
  cal.get(Calendar.MONTH),
  cal.get(Calendar.DATE)));
  }
 
  if (request.getGender() == AdRequest.GENDER_FEMALE) {
  adRequest.setGender(TWMAdRequest.Gender.FEMALE);
  } else if (request.getGender() == AdRequest.GENDER_MALE) {
  adRequest.setGender(TWMAdRequest.Gender.MALE);
  } else {
  adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
  }
 
  return adRequest;
  }
 
  <b>//將TAMedia ErrorCode轉換為AdMob的ErrorCode</b>
  private int convertErrorCode(ErrorCode errorCode){
  if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
  return AdRequest.ERROR_CODE_INTERNAL_ERROR;
  }else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
  return AdRequest.ERROR_CODE_INVALID_REQUEST;
  }else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
  return AdRequest.ERROR_CODE_NETWORK_ERROR;
  }else if(errorCode.equals(ErrorCode.NO_FILL)){
  return AdRequest.ERROR_CODE_NO_FILL;
  }else
  return AdRequest.ERROR_CODE_NETWORK_ERROR;
  }
 
  private int getAgeByBirthday(int year, int month, int day){
  Calendar dob = Calendar.getInstance();
  Calendar today = Calendar.getInstance();
 
  dob.set(year, month, day);
 
  int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
  if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
  age--;
  }
 
  Integer ageInt = new Integer(age);
  return ageInt.intValue();
  }
  }
<br><br><br>
== '''範例程式碼''' ==
:<p style="font-size: 18px;">[[媒體:TAMedia - AdMob Medation.zip  | Download Sample Code]]</p>
:<p style="font-size: 18px;">以下以Banner為例:</p>
::<p style="font-size: 18px;">1. 在layout檔案中加入可放置廣告的LinearLayout(※僅供參考,開發者可依照自訂的layout進行配置使用)</p>
<LinearLayout
    android:id="@+id/Layout_AdView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
::<p style="font-size: 18px;">2. 程式加入Banner的宣告, 設定AdUnitId與AdSize</p>
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("<b><AdMob Banner Ad Unit ID></b>");
        adView.setAdSize(AdSize.BANNER);   
        layout.addView(adView);
        AdRequest request = new AdRequest.Builder().build();
        adView.loadAd(request);
    }
}
::<p style="font-size: 18px;">程式內的<b><AdMob Banner Ad Unit ID></b>為 '''[[#新增廣告單元|新增廣告單元]]''' 最後取得的AdMob廣告單元ID</p>
<br><br><br>
<p style="font-size: 18px;">[[Android SDK Developer Guide 2| 回首頁]]</p>

於 2019年7月10日 (三) 07:14 的修訂

AdMob網站: https://apps.admob.com/

新增應用程式

  • 進入 AdMob 網頁選擇 應用程式 > 新增應用程式

  • Admob application 00.PNG


  • 輸入應用程式名稱並選擇平台

  • Admob application 01.PNG


  • 建立完成,請記下應用程式 ID

  • Admob application 02.PNG





新增廣告單元

  • 點選先前建立的應用程式並選擇 廣告單元 > 開始匯入

  • Admob adunit 00.PNG


  • 選擇欲建立的廣告單元 (以橫幅廣告為例)

  • Admob adunit 01.PNG


  • 輸入廣告單元名稱

  • Admob adunit 02.PNG


  • 建立完成,請記下 AdMob 的廣告單元ID

  • Admob adunit 03.PNG






新增中介服務群組

  • 進入 AdMob 網頁選擇 中介服務 > 新增中介服務群組

  • Admob medgroup 00.PNG


  • 設定廣告格式、平台

  • Admob medgroup 01.PNG


  • 設定中介群組名稱並點選加入廣告單元

  • Admob medgroup 02.PNG


  • 選擇先前建立的廣告單元 (以橫幅廣告為例)

  • Admob medgroup 03.PNG


  • 點選新增自訂事件,並設定下列相關資訊:

    有效千次曝光出價: 越高代表該家聯播網廣告曝光的機會越高

    Class Name: 根據您的專案輸入 mediation code 的 package name,如: com.taiwanmobile.pt.adp.mediation.TAMediaBanner (下方實作 Mediation Code章節會說明Mediation Code的實作方式)

    Parameter: 輸入您的 TAMedia 廣告版位ID

  • Admob medgroup 04.PNG

    Admob medgroup 05.PNG

    Admob medgroup 06.PNG


  • 點選完成後,中介服務群組的設置便結束

  • Admob medgroup 07.PNG





程式整合

Getting Started

1. 請參考AdMob for Android網站提供的方式整合Google Ads SDK,會使用到新增應用程式所取得的應用程式ID

2. 下載 TAMedia Android SDK,並參考 開始使用Getting Started整合TAMedia Android SDK





實作 Mediation Code

依照下列方式實作AdMob所提供的CustomEventBanner

 package com.taiwanmobile.pt.adp.mediation;
 
 import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.util.Log;
 
 import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.AdSize;
 import com.google.android.gms.ads.mediation.MediationAdRequest;
 import com.google.android.gms.ads.mediation.customevent.CustomEventBanner;
 import com.google.android.gms.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 java.util.Calendar;
 import java.util.Date;
 
 // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於中介服務群組, 如 新增中介服務群組 所述
 public class TAMediaBanner implements CustomEventBanner {
     
     private static final String TAG = "TAMediaBanner";
     
     private TWMAdView adView = null;
 
     @Override
     public void requestBannerAd(final Context context,
                                 final CustomEventBannerListener customEventBannerListener,
                                 String serverParameter,
                                 AdSize adSize,
                                 MediationAdRequest mediationAdRequest,
                                 Bundle bundle) {
         // serverParameter: AdMob 後台設定的 TAMedia Slot ID, 會藉由此參數獲得
         Log.d(TAG, "requestBannerAd("+serverParameter+") invoked!!");
 
         adView = new TWMAdView( (Activity) context, convertAdSize(adSize), serverParameter);
         adView.setAdListener(new TWMAdViewListener(){
             @Override
             public void onReceiveAd(TWMAd ad) {
                 ((Activity) context).runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         customEventBannerListener.onAdLoaded(adView);
                     }
                 });
             }
 
             @Override
             public void onFailedToReceiveAd(TWMAd ad, final ErrorCode errorCode) {
                 ((Activity) context).runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         customEventBannerListener.onAdFailedToLoad(convertErrorCode(errorCode));
                     }
                 });
             }
 
             @Override
             public void onPresentScreen(TWMAd ad) {
                 ((Activity) context).runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         customEventBannerListener.onAdOpened();
                         //若您需要在AdMob報表上觀察點擊率, 請加入CustomEventBannerListener.onClick()
                         customEventBannerListener.onAdClicked();
                     }
                 });
 
             }
 
             @Override
             public void onDismissScreen(TWMAd ad) {
                 ((Activity) context).runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         customEventBannerListener.onAdClosed();
                     }
                 });
             }
 
             @Override
             public void onLeaveApplication(TWMAd ad) {
                 ((Activity) context).runOnUiThread(new Runnable() {
                     @Override
                     public void run() {
                         customEventBannerListener.onAdLeftApplication();
                     }
                 });
             }
         });
 
         adView.loadAd(convertAdRequest(mediationAdRequest));
     }
 
     @Override
     public void onDestroy() {
         if(adView != null){
             adView.destroy();
         }
     }
 
     @Override
     public void onPause() {}
 
     @Override
     public void onResume() {}
 
 
     //將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
     private TWMAdRequest convertAdRequest(MediationAdRequest request) {
         TWMAdRequest adRequest = new TWMAdRequest();
         if (request.getBirthday() != null) {
 
             Date bDay = request.getBirthday();
             adRequest.setBirthday(bDay);
 
             Calendar cal = Calendar.getInstance();
             cal.setTime(bDay);
             adRequest.setAge(getAgeByBirthday(
                     cal.get(Calendar.YEAR),
                     cal.get(Calendar.MONTH),
                     cal.get(Calendar.DATE)));
         }
 
         if (request.getGender() == AdRequest.GENDER_FEMALE) {
             adRequest.setGender(TWMAdRequest.Gender.FEMALE);
         } else if (request.getGender() == 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 (isAdSizeEqual(adSize, TWMAdSize.BANNER)) {
             return TWMAdSize.BANNER;
         } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_MRECT)) {
             return TWMAdSize.IAB_MRECT;
         } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_BANNER)) {
             return TWMAdSize.IAB_BANNER;
         } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_LEADERBOARD)) {
             return TWMAdSize.IAB_LEADERBOARD;
         } else if (isAdSizeEqual(adSize, TWMAdSize.IAB_WIDE_SKYSCRAPER)) {
             return TWMAdSize.IAB_WIDE_SKYSCRAPER;
         } else if (isAdSizeEqual(adSize, TWMAdSize.SMART_BANNER)) {
             return TWMAdSize.SMART_BANNER;
         } else {
             return TWMAdSize.BANNER;
         }
     }
 
     private boolean isAdSizeEqual(AdSize adSize, TWMAdSize twmAdSize) {
         return (adSize.getWidth() == twmAdSize.getWidth()
                 && adSize.getHeight() == twmAdSize.getHeight());
     }
 
     //將TAMedia ErrorCode轉換為AdMob的ErrorCode
     private int convertErrorCode(ErrorCode errorCode){
         if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
             return AdRequest.ERROR_CODE_INTERNAL_ERROR;
         }else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
             return AdRequest.ERROR_CODE_INVALID_REQUEST;
         }else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
             return AdRequest.ERROR_CODE_NETWORK_ERROR;
         }else if(errorCode.equals(ErrorCode.NO_FILL)){
             return AdRequest.ERROR_CODE_NO_FILL;
         }else
             return AdRequest.ERROR_CODE_NETWORK_ERROR;
     }
 
     private int getAgeByBirthday(int year, int month, int day){
         Calendar dob = Calendar.getInstance();
         Calendar today = Calendar.getInstance();
 
         dob.set(year, month, day);
 
         int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
         if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
             age--;
         }
 
         Integer ageInt = new Integer(age);
         return ageInt.intValue();
     }
 
 }




Interstitial

依照下列方式實作AdMob所提供的CustomEventInterstitial

 package com.taiwanmobile.pt.adp.mediation;
 
 import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.util.Log;
 
 
 import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.mediation.MediationAdRequest;
 import com.google.android.gms.ads.mediation.customevent.CustomEventInterstitial;
 import com.google.android.gms.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;
 
 import java.util.Calendar;
 import java.util.Date;
 
 // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaInterstitial註冊於中介服務群組, 如 新增中介服務群組 所述
 public class TAMediaInterstitial implements CustomEventInterstitial {
 
 	private TWMInterstitialAd interstitialAd = null;
 
 	private static final String TAG ="TAMediaInterstitial";
 
 	@Override
 	public void requestInterstitialAd(final Context context,
 					final CustomEventInterstitialListener customEventInterstitialListener,
 					String serverParameter,
 					MediationAdRequest mediationAdRequest,
 					Bundle bundle) {
 		// serverParameter: AdMob 後台設定的 TAMedia Slot ID, 會藉由此參數獲得
 		Log.d(TAG, "requestInterstitialAd("+serverParameter+") ");
 
 		interstitialAd = new TWMInterstitialAd((Activity)context, serverParameter);
 
 		interstitialAd.setAdListener(new TWMAdViewListener() {
 
 			@Override
 			public void onReceiveAd(TWMAd ad) {
 				((Activity) context).runOnUiThread(new Runnable() {
 						@Override
 						public void run() {
 						customEventInterstitialListener.onAdLoaded();
 					}
 				});
 			}
 
 			@Override
 			public void onFailedToReceiveAd(TWMAd ad, final ErrorCode errorCode) {
 				((Activity) context).runOnUiThread(new Runnable() {
 					@Override
 					public void run() {
 						customEventInterstitialListener.onAdFailedToLoad(convertErrorCode(errorCode));
 					}
 				});
 			}
 
 			@Override
 			public void onPresentScreen(TWMAd ad) {
 				((Activity) context).runOnUiThread(new Runnable() {
 					@Override
 					public void run() {
 						customEventInterstitialListener.onAdOpened();
 					}
 				});
 			}
 
 			@Override
 			public void onDismissScreen(TWMAd ad) {
 				((Activity) context).runOnUiThread(new Runnable() {
 					@Override
 					public void run() {
 						customEventInterstitialListener.onAdClosed();
 					}
 				});
 			}
 
 			@Override
 			public void onLeaveApplication(TWMAd ad) {
 				((Activity) context).runOnUiThread(new Runnable() {
 					@Override
 					public void run() {
 						customEventInterstitialListener.onAdLeftApplication();
 					}
 				});
 			}
 
 		});
 
 		interstitialAd.loadAd(convertAdRequest(mediationAdRequest));
 	}
 
 	@Override
 	public void showInterstitial() {
 		if (interstitialAd != null) {
 			interstitialAd.show();
 		}
 	}
 
 	@Override
 	public void onDestroy() {}
 
 	@Override
 	public void onPause() {}
 
 	@Override
 	public void onResume() {}
 
 	//將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
 	private TWMAdRequest convertAdRequest(MediationAdRequest request) {
 		TWMAdRequest adRequest = new TWMAdRequest();
 		if (request.getBirthday() != null) {
 
 			Date bDay = request.getBirthday();
 			adRequest.setBirthday(bDay);
 
 			Calendar cal = Calendar.getInstance();
 			cal.setTime(bDay);
 			adRequest.setAge(getAgeByBirthday(
 					cal.get(Calendar.YEAR),
 					cal.get(Calendar.MONTH),
 					cal.get(Calendar.DATE)));
 		}
 
 		if (request.getGender() == AdRequest.GENDER_FEMALE) {
 			adRequest.setGender(TWMAdRequest.Gender.FEMALE);
 		} else if (request.getGender() == AdRequest.GENDER_MALE) {
 			adRequest.setGender(TWMAdRequest.Gender.MALE);
 		} else {
 			adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
 		}
 
 		return adRequest;
 	}
 
 	//將TAMedia ErrorCode轉換為AdMob的ErrorCode
 	private int convertErrorCode(ErrorCode errorCode){
 		if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
 			return AdRequest.ERROR_CODE_INTERNAL_ERROR;
 		}else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
 			return AdRequest.ERROR_CODE_INVALID_REQUEST;
 		}else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
 			return AdRequest.ERROR_CODE_NETWORK_ERROR;
 		}else if(errorCode.equals(ErrorCode.NO_FILL)){
 			return AdRequest.ERROR_CODE_NO_FILL;
 		}else
 			return AdRequest.ERROR_CODE_NETWORK_ERROR;
 	}
 
 	private int getAgeByBirthday(int year, int month, int day){
 		Calendar dob = Calendar.getInstance();
 		Calendar today = Calendar.getInstance();
 
 		dob.set(year, month, day);
 
 		int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
 		if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
 			age--;
 		}
 
 		Integer ageInt = new Integer(age);
 		return ageInt.intValue();
 	}
 }






範例程式碼

Download Sample Code

以下以Banner為例:

1. 在layout檔案中加入可放置廣告的LinearLayout(※僅供參考,開發者可依照自訂的layout進行配置使用)

<LinearLayout
   android:id="@+id/Layout_AdView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

2. 程式加入Banner的宣告, 設定AdUnitId與AdSize

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("<AdMob Banner Ad Unit ID>");
       adView.setAdSize(AdSize.BANNER);    
       layout.addView(adView);
       AdRequest request = new AdRequest.Builder().build();
       adView.loadAd(request);
   }
}

程式內的<AdMob Banner Ad Unit ID>新增廣告單元 最後取得的AdMob廣告單元ID





回首頁