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

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
 
(未顯示同一使用者於中間所作的 10 次修訂)
行 1: 行 1:
AdMob網站: https://apps.admob.com/ <br>
AdMob網站: https://apps.admob.com/ <br>
Google Play Service Version : 6.5.87+ <br>
<br>


== <b>新增廣告單元</b>  ==
== <b>新增廣告單元</b>  ==
:以下以Banner為例:<br>
:以下以Banner為例:<br>
:點選 <b>"營利"</b>, 並點選 <b>"透過新應用程式營利"</b><br>
:點選 <b>"營利"</b>, 並點選 <b>"透過新應用程式營利"</b><br>
:[[image:2017admob_step1-1.png | 500px]]<br>
:[[image:2017admob_step1-1.png]]<br>
<br>
<br>
:點選 <b>"手動新增應用程式"</b> , 輸入您的 <b>"應用程式名稱"</b> 並選擇 <b>"Android"</b> 平台, 完成點選 <b>"新增應用程式"</b><br>
:點選 <b>"手動新增應用程式"</b> , 輸入您的 <b>"應用程式名稱"</b> 並選擇 <b>"Android"</b> 平台, 完成點選 <b>"新增應用程式"</b><br>
:[[image:2017admob_step1-2.png | 400px]]<br>
:[[image:2017admob_step1-2.png ]]<br>
<br>
<br>
:根據選項設定您的廣告單元, 並輸入 <b>"廣告單元名稱"</b> , 完成後點選 <b>"儲存"</b><br>
:根據選項設定您的廣告單元, 並輸入 <b>"廣告單元名稱"</b> , 完成後點選 <b>"儲存"</b><br>
:[[image:2017admob_step1-3.png | 500px]]<br>
:[[image:2017admob_step1-3.png]]<br>
<br>
<br>
:若無使用Firebase, 可直接選擇略過
:若無使用Firebase, 可直接選擇略過
:[[image:2017admob_step1-4.png | 500px]]<br>
:[[image:2017admob_step1-4.png]]<br>
<br>
<br>
:記下AdMob的廣告單元ID後, 點選 <b>"完成"</b> , 即完成廣告單元的建置<br>
:記下AdMob的廣告單元ID後, 點選 <b>"完成"</b> , 即完成廣告單元的建置<br>
:[[image:2017admob_step1-5.png | 500px]]<br>
:[[image:2017admob_step1-5.png]]<br>
<br>
<br>
<br>
<br>
行 24: 行 22:
== <b>新增廣告聯播網</b> ==
== <b>新增廣告聯播網</b> ==
:點選 <b>"營利"</b> , 選擇應用程式後, 再點選廣告單元中的 <b>"廣告來源"</b>
:點選 <b>"營利"</b> , 選擇應用程式後, 再點選廣告單元中的 <b>"廣告來源"</b>
:[[image:2017admob_step2-1.png | 800px]]<br>
:[[image:2017admob_step2-1.png]]<br>
<br>
<br>
:點選 "新增廣告聯播網" (AdMob Network為預設產生)<br>
:點選 "新增廣告聯播網" (AdMob Network為預設產生)<br>
:[[image:2017admob_step2-2.png | 500px]]<br>
:[[image:2017admob_step2-2.png]]<br>
<br>
<br>
:點選 "自訂事件" , 依欄位填入
:點選 "自訂事件" , 依欄位填入
行 35: 行 33:
<li>Parameter: 請填入您的TAMedia版位ID (可向您的TAMedia窗口詢問)</li>
<li>Parameter: 請填入您的TAMedia版位ID (可向您的TAMedia窗口詢問)</li>
</ol>
</ol>
:[[image:2017admob_step2-3.png | 800px]]<br>
:[[image:2017admob_step2-3.png]]<br>
<br>
<br>
<br>
<br>
行 41: 行 39:
== <b>程式整合-Getting Started</b> ==
== <b>程式整合-Getting Started</b> ==
:1. 請參考[https://developers.google.com/admob/android/quick-start?hl=zh-TW AdMob for Android]網站提供的方式整合Google Ads SDK<br>
:1. 請參考[https://developers.google.com/admob/android/quick-start?hl=zh-TW AdMob for Android]網站提供的方式整合Google Ads SDK<br>
:2. 下載[[Download Android SDK 2 | TAMedia Android SDK]], 並參考 [[Android Getting Started 2.0 | "開始使用Getting Started"]] 整合TAMedia Android SDK<br>
:2. 下載[[Download Android SDK 6 | TAMedia Android SDK]], 並參考 [[Android Getting Started 3.0 | "開始使用Getting Started"]] 整合TAMedia Android SDK<br>
:3. 加入必要的權限<br>
:3. 加入必要的權限<br>
::TWMAd服務需使用 "存取網路狀態"、"讀取約略位置"、"網路" 等權限,請在AndroidManifest.xml檔案當中進行下列權限的宣告。
::TWMAd服務需使用 "存取網路狀態"、"讀取約略位置"、"網路" 等權限,請在AndroidManifest.xml檔案當中進行下列權限的宣告。
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.READ_PHONE_STATE" /> &lt;!-- MADP SDK4.0以上版本請移除 --&gt;
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- optional -->
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> &lt;!-- optional --&gt;
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- optional -->
  <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_WIFI_STATE" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  
  <uses-permission android:name="android.permission.VIRBATE" /> <!-- optional -->
  <uses-permission android:name="android.permission.VIBRATE" /> &lt;!-- optional --&gt;
 
:4. 加入必要的Activity
:4. 加入必要的Activity
  &lt;!-- TAMedia Activities --&gt;
  &lt;!-- TAMedia Activities --&gt;
行 95: 行 94:
   import java.util.Date;
   import java.util.Date;
    
    
   // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述
   <b>// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaBanner註冊於mediation的設定頁面上, 如前述章節所述</b>
   public class TAMediaBanner implements CustomEventBanner {
   public class TAMediaBanner implements CustomEventBanner {
   
     
    private static final String TAG = "TAMediaBanner";
      private static final String TAG = "TAMediaBanner";
   
     
    private TWMAdView adView = null;
      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!!");
    
    
    @Override
          adView = new TWMAdView( (Activity) context, convertAdSize(adSize), serverParameter);
    public void requestBannerAd(final Context context,
          adView.setAdListener(new TWMAdViewListener(){
                          final CustomEventBannerListener customEventBannerListener,
              @Override
                          String serverParameter,
              public void onReceiveAd(TWMAd ad) {
                          AdSize adSize,
                  ((Activity) context).runOnUiThread(new Runnable() {
                          MediationAdRequest mediationAdRequest,
                      @Override
                          Bundle bundle) {
                      public void run() {
        // serverParameter: AdMob 後台設定的 TAMedia Slot ID, 會藉由此參數獲得
                          customEventBannerListener.onAdLoaded(adView);
        Log.d(TAG, "requestBannerAd("+serverParameter+") invoked!!");
                      }
                  });
              }
    
    
        adView = new TWMAdView( (Activity) context, convertAdSize(adSize), serverParameter);
              @Override
        adView.setAdListener(new TWMAdViewListener(){
              public void onFailedToReceiveAd(TWMAd ad, final ErrorCode errorCode) {
          @Override
                  ((Activity) context).runOnUiThread(new Runnable() {
          public void onReceiveAd(TWMAd ad) {
                      @Override
              customEventBannerListener.onAdLoaded(adView);
                      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 onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
               customEventBannerListener.onAdFailedToLoad(convertErrorCode(errorCode));
          }
    
    
          @Override
              @Override
          public void onPresentScreen(TWMAd ad) {
              public void onDismissScreen(TWMAd ad) {
              customEventBannerListener.onAdOpened();
                  ((Activity) context).runOnUiThread(new Runnable() {
              //若您需要在AdMob報表上觀察點擊率, 請加入CustomEventBannerListener.onClick()
                      @Override
              customEventBannerListener.onAdClicked();
                      public void run() {
                          customEventBannerListener.onAdClosed();
                      }
                  });
              }
    
    
          }
              @Override
              public void onLeaveApplication(TWMAd ad) {
                  ((Activity) context).runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          customEventBannerListener.onAdLeftApplication();
                      }
                  });
              }
          });
    
    
          @Override
          adView.loadAd(convertAdRequest(mediationAdRequest));
          public void onDismissScreen(TWMAd ad) {
      }
              customEventBannerListener.onAdClosed();
          }
    
    
          @Override
      @Override
          public void onLeaveApplication(TWMAd ad) {
      public void onDestroy() {
               customEventBannerListener.onAdLeftApplication();
          if(adView != null){
          }
               adView.destroy();
        });
          }
      }
    
    
        adView.loadAd(convertAdRequest(mediationAdRequest));
      @Override
    }
      public void onPause() {}
    
    
    @Override
      @Override
    public void onDestroy() {
      public void onResume() {}
        if(adView != null){
          adView.destroy();
        }
    }
    
    
    @Override
    public void onPause() {}
    
    
    @Override
      <b>//將AdMob所提供的MediationAdRequest轉換為TWMAdRequest</b>
    public void onResume() {}
      private TWMAdRequest convertAdRequest(MediationAdRequest request) {
          TWMAdRequest adRequest = new TWMAdRequest();
          if (request.getBirthday() != null) {
    
    
    //將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
              Date bDay = request.getBirthday();
    private TWMAdRequest convertAdRequest(MediationAdRequest request) {
              adRequest.setBirthday(bDay);
        TWMAdRequest adRequest = new TWMAdRequest();
        if (request.getBirthday() != null) {
    
    
          Date bDay = request.getBirthday();
              Calendar cal = Calendar.getInstance();
          adRequest.setBirthday(bDay);
              cal.setTime(bDay);
              adRequest.setAge(getAgeByBirthday(
                      cal.get(Calendar.YEAR),
                      cal.get(Calendar.MONTH),
                      cal.get(Calendar.DATE)));
          }
    
    
          Calendar cal = Calendar.getInstance();
          if (request.getGender() == AdRequest.GENDER_FEMALE) {
          cal.setTime(bDay);
              adRequest.setGender(TWMAdRequest.Gender.FEMALE);
          adRequest.setAge(getAgeByBirthday(
          } else if (request.getGender() == AdRequest.GENDER_MALE) {
                cal.get(Calendar.YEAR),
              adRequest.setGender(TWMAdRequest.Gender.MALE);
                cal.get(Calendar.MONTH),
          } else {
                cal.get(Calendar.DATE)));
              adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
        }
          }
    
    
        if (request.getGender() == AdRequest.GENDER_FEMALE) {
          return adRequest;
          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;
          }
      }
    
    
    //將AdMob的AdSize轉換為TWMAdSize
      private boolean isAdSizeEqual(AdSize adSize, TWMAdSize twmAdSize) {
    private TWMAdSize convertAdSize(AdSize adSize) {
          return (adSize.getWidth() == twmAdSize.getWidth()
        if(adSize.equals(AdSize.BANNER)){
                  && adSize.getHeight() == twmAdSize.getHeight());
          return TWMAdSize.BANNER;
      }
        }else if(adSize.equals(AdSize.FULL_BANNER)){
          return TWMAdSize.IAB_BANNER;
        }else if(adSize.equals(AdSize.MEDIUM_RECTANGLE)){
          return TWMAdSize.IAB_MRECT;
        }else if(adSize.equals(AdSize.LEADERBOARD)){
          return TWMAdSize.IAB_LEADERBOARD;
        }else if(adSize.equals(AdSize.SMART_BANNER)){
          return TWMAdSize.SMART_BANNER;
        }else{
          return TWMAdSize.BANNER;
        }
    }
    
    
    //將TAMedia ErrorCode轉換為AdMob的ErrorCode
      <b>//將TAMedia ErrorCode轉換為AdMob的ErrorCode</b>
    private int convertErrorCode(ErrorCode errorCode){
      private int convertErrorCode(ErrorCode errorCode){
        if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
          if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
          return AdRequest.ERROR_CODE_INTERNAL_ERROR;
              return AdRequest.ERROR_CODE_INTERNAL_ERROR;
        }else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
          }else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
          return AdRequest.ERROR_CODE_INVALID_REQUEST;
              return AdRequest.ERROR_CODE_INVALID_REQUEST;
        }else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
          }else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
          return AdRequest.ERROR_CODE_NETWORK_ERROR;
              return AdRequest.ERROR_CODE_NETWORK_ERROR;
        }else if(errorCode.equals(ErrorCode.NO_FILL)){
          }else if(errorCode.equals(ErrorCode.NO_FILL)){
          return AdRequest.ERROR_CODE_NO_FILL;
              return AdRequest.ERROR_CODE_NO_FILL;
        }else
          }else
          return AdRequest.ERROR_CODE_NETWORK_ERROR;
              return AdRequest.ERROR_CODE_NETWORK_ERROR;
    }
      }
    
    
    private int getAgeByBirthday(int year, int month, int day){
      private int getAgeByBirthday(int year, int month, int day){
        Calendar dob = Calendar.getInstance();
          Calendar dob = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
          Calendar today = Calendar.getInstance();
    
    
        dob.set(year, month, day);
          dob.set(year, month, day);
    
    
        int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
          int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
          if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
          age--;
              age--;
        }
          }
    
    
        Integer ageInt = new Integer(age);
          Integer ageInt = new Integer(age);
        return ageInt.intValue();
          return ageInt.intValue();
    }
      }
    
    
   }
   }
<br>
<br>
=== Interstitial ===
=== Interstitial ===
行 258: 行 290:
   import java.util.Date;
   import java.util.Date;
    
    
   // 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaInterstitial註冊於mediation的設定頁面上, 如前述章節所述
   <b>// 需將本class的完整類別名稱 com.taiwanmobile.pt.adp.mediation.TAMediaInterstitial註冊於mediation的設定頁面上, 如前述章節所述</b>
   public class TAMediaInterstitial implements CustomEventInterstitial {
   public class TAMediaInterstitial implements CustomEventInterstitial {
    
    
    private TWMInterstitialAd interstitialAd = null;
  private TWMInterstitialAd interstitialAd = null;
    
    
    private static final String TAG ="TAMediaInterstitial";
  private static final String TAG ="TAMediaInterstitial";
    
    
    @Override
  @Override
    public void requestInterstitialAd(Context context,
  public void requestInterstitialAd(final Context context,
                              final CustomEventInterstitialListener customEventInterstitialListener,
  final CustomEventInterstitialListener customEventInterstitialListener,
                              String serverParameter,
  String serverParameter,
                              MediationAdRequest mediationAdRequest,
  MediationAdRequest mediationAdRequest,
                              Bundle bundle) {
  Bundle bundle) {
        // serverParameter: AdMob 後台設定的 TAMedia Slot ID, 會藉由此參數獲得
  <b>// serverParameter: AdMob 後台設定的 TAMedia Slot ID, 會藉由此參數獲得</b>
        Log.d(TAG, "requestInterstitialAd("+serverParameter+") ");
  Log.d(TAG, "requestInterstitialAd("+serverParameter+") ");
    
    
        interstitialAd = new TWMInterstitialAd((Activity)context, serverParameter);
  interstitialAd = new TWMInterstitialAd((Activity)context, serverParameter);
    
    
        interstitialAd.setAdListener(new TWMAdViewListener() {
  interstitialAd.setAdListener(new TWMAdViewListener() {
    
    
          @Override
  @Override
          public void onReceiveAd(TWMAd ad) {
  public void onReceiveAd(TWMAd ad) {
              customEventInterstitialListener.onAdLoaded();
  ((Activity) context).runOnUiThread(new Runnable() {
          }
  @Override
  public void run() {
  customEventInterstitialListener.onAdLoaded();
  }
  });
  }
    
    
          @Override
  @Override
          public void onFailedToReceiveAd(TWMAd ad, ErrorCode errorCode) {
  public void onFailedToReceiveAd(TWMAd ad, final ErrorCode errorCode) {
              customEventInterstitialListener.onAdFailedToLoad(convertErrorCode(errorCode));
  ((Activity) context).runOnUiThread(new Runnable() {
          }
  @Override
  public void run() {
  customEventInterstitialListener.onAdFailedToLoad(convertErrorCode(errorCode));
  }
  });
  }
    
    
          @Override
  @Override
          public void onPresentScreen(TWMAd ad) {
  public void onPresentScreen(TWMAd ad) {
              customEventInterstitialListener.onAdOpened();
  ((Activity) context).runOnUiThread(new Runnable() {
          }
  @Override
  public void run() {
  customEventInterstitialListener.onAdOpened();
  }
  });
  }
    
    
          @Override
  @Override
          public void onDismissScreen(TWMAd ad) {
  public void onDismissScreen(TWMAd ad) {
              customEventInterstitialListener.onAdClosed();
  ((Activity) context).runOnUiThread(new Runnable() {
          }
  @Override
  public void run() {
  customEventInterstitialListener.onAdClosed();
  }
  });
  }
    
    
          @Override
  @Override
          public void onLeaveApplication(TWMAd ad) {
  public void onLeaveApplication(TWMAd ad) {
              customEventInterstitialListener.onAdLeftApplication();
  ((Activity) context).runOnUiThread(new Runnable() {
          }
  @Override
  public void run() {
  customEventInterstitialListener.onAdLeftApplication();
  }
  });
  }
    
    
        });
  });
    
    
        interstitialAd.loadAd(convertAdRequest(mediationAdRequest));
  interstitialAd.loadAd(convertAdRequest(mediationAdRequest));
    }
  }
    
    
    @Override
  @Override
    public void showInterstitial() {
  public void showInterstitial() {
        if (interstitialAd != null) {
  if (interstitialAd != null) {
          interstitialAd.show();
  interstitialAd.show();
        }
  }
    }
  }
    
    
    @Override
  @Override
    public void onDestroy() {}
  public void onDestroy() {}
    
    
    @Override
  @Override
    public void onPause() {}
  public void onPause() {}
    
    
    @Override
  @Override
    public void onResume() {}
  public void onResume() {}
    
    
    //將AdMob所提供的MediationAdRequest轉換為TWMAdRequest
  <b>//將AdMob所提供的MediationAdRequest轉換為TWMAdRequest</b>
    private TWMAdRequest convertAdRequest(MediationAdRequest request) {
  private TWMAdRequest convertAdRequest(MediationAdRequest request) {
        TWMAdRequest adRequest = new TWMAdRequest();
  TWMAdRequest adRequest = new TWMAdRequest();
        if (request.getBirthday() != null) {
  if (request.getBirthday() != null) {
    
    
          Date bDay = request.getBirthday();
  Date bDay = request.getBirthday();
          adRequest.setBirthday(bDay);
  adRequest.setBirthday(bDay);
    
    
          Calendar cal = Calendar.getInstance();
  Calendar cal = Calendar.getInstance();
          cal.setTime(bDay);
  cal.setTime(bDay);
          adRequest.setAge(getAgeByBirthday(
  adRequest.setAge(getAgeByBirthday(
                cal.get(Calendar.YEAR),
  cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH),
  cal.get(Calendar.MONTH),
                cal.get(Calendar.DATE)));
  cal.get(Calendar.DATE)));
        }
  }
    
    
        if (request.getGender() == AdRequest.GENDER_FEMALE) {
  if (request.getGender() == AdRequest.GENDER_FEMALE) {
          adRequest.setGender(TWMAdRequest.Gender.FEMALE);
  adRequest.setGender(TWMAdRequest.Gender.FEMALE);
        } else if (request.getGender() == AdRequest.GENDER_MALE) {
  } else if (request.getGender() == AdRequest.GENDER_MALE) {
          adRequest.setGender(TWMAdRequest.Gender.MALE);
  adRequest.setGender(TWMAdRequest.Gender.MALE);
        } else {
  } else {
          adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
  adRequest.setGender(TWMAdRequest.Gender.UNKNOWN);
        }
  }
    
    
        return adRequest;
  return adRequest;
    }
  }
    
    
    //將TAMedia ErrorCode轉換為AdMob的ErrorCode
  <b>//將TAMedia ErrorCode轉換為AdMob的ErrorCode</b>
    private int convertErrorCode(ErrorCode errorCode){
  private int convertErrorCode(ErrorCode errorCode){
        if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
  if(errorCode.equals(ErrorCode.INTERNAL_ERROR)){
          return AdRequest.ERROR_CODE_INTERNAL_ERROR;
  return AdRequest.ERROR_CODE_INTERNAL_ERROR;
        }else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
  }else if(errorCode.equals(ErrorCode.INVALID_REQUEST)){
          return AdRequest.ERROR_CODE_INVALID_REQUEST;
  return AdRequest.ERROR_CODE_INVALID_REQUEST;
        }else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
  }else if(errorCode.equals(ErrorCode.NETWORK_ERROR)){
          return AdRequest.ERROR_CODE_NETWORK_ERROR;
  return AdRequest.ERROR_CODE_NETWORK_ERROR;
        }else if(errorCode.equals(ErrorCode.NO_FILL)){
  }else if(errorCode.equals(ErrorCode.NO_FILL)){
          return AdRequest.ERROR_CODE_NO_FILL;
  return AdRequest.ERROR_CODE_NO_FILL;
        }else
  }else
          return AdRequest.ERROR_CODE_NETWORK_ERROR;
  return AdRequest.ERROR_CODE_NETWORK_ERROR;
    }
  }
    
    
    private int getAgeByBirthday(int year, int month, int day){
  private int getAgeByBirthday(int year, int month, int day){
        Calendar dob = Calendar.getInstance();
  Calendar dob = Calendar.getInstance();
        Calendar today = Calendar.getInstance();
  Calendar today = Calendar.getInstance();
    
    
        dob.set(year, month, day);
  dob.set(year, month, day);
    
    
        int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
  int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
  if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
          age--;
  age--;
        }
  }
    
    
        Integer ageInt = new Integer(age);
  Integer ageInt = new Integer(age);
        return ageInt.intValue();
  return ageInt.intValue();
    }
  }
   }
   }
<br>
<br>
<br>
<br>
== 範例程式碼 ==
== 範例程式碼 ==
:[[媒體:TAMedia - AdMob Medation.zip  | Download Sample Code (For Android Studio)]]
:[[媒體:TAMedia - AdMob Medation.zip  | Download Sample Code (For Android Studio)]]<br>
:[[媒體:TAMedia_-_AdMob_Medation_Eclipse.zip | Download Sample Code (For Eclipse)]]<br><br>
:以下以Banner為例:<br>
:以下以Banner為例:<br>
:1. 在layout檔案中加入可放置廣告的LinearLayout(※僅供參考,開發者可依照自訂的layout進行配置使用)
:1. 在layout檔案中加入可放置廣告的LinearLayout(※僅供參考,開發者可依照自訂的layout進行配置使用)
行 407: 行 465:
  }
  }
:程式內的<b><AdMob Banner Ad Unit ID></b>為 <b>"新增廣告單元"</b> 最後取得的AdMob廣告單元ID<br>
:程式內的<b><AdMob Banner Ad Unit ID></b>為 <b>"新增廣告單元"</b> 最後取得的AdMob廣告單元ID<br>
:[[image:2017admob_step5-1.png | 500px]]<br>
:[[image:2017admob_step5-1.png]]<br>
<br>
<br>
[[Android SDK Developer Guide 2 | 回首頁]]
[[Android SDK Developer Guide 2 | 回首頁]]

於 2018年11月14日 (三) 09:34 的最新修訂

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

新增廣告單元

以下以Banner為例:
點選 "營利", 並點選 "透過新應用程式營利"
2017admob step1-1.png


點選 "手動新增應用程式" , 輸入您的 "應用程式名稱" 並選擇 "Android" 平台, 完成點選 "新增應用程式"
2017admob step1-2.png


根據選項設定您的廣告單元, 並輸入 "廣告單元名稱" , 完成後點選 "儲存"
2017admob step1-3.png


若無使用Firebase, 可直接選擇略過
2017admob step1-4.png


記下AdMob的廣告單元ID後, 點選 "完成" , 即完成廣告單元的建置
2017admob step1-5.png



新增廣告聯播網

點選 "營利" , 選擇應用程式後, 再點選廣告單元中的 "廣告來源"
2017admob step2-1.png


點選 "新增廣告聯播網" (AdMob Network為預設產生)
2017admob step2-2.png


點選 "自訂事件" , 依欄位填入
  1. Class Name: 用於實作 com.google.android.gms.ads.mediation.customevent.CustomEventBanner 的類別名稱, 必須輸入完整的package name, 如com.taiwanmobile.pt.adp.mediation.TAMediaBanner
  2. Label: 任意自定的名稱
  3. Parameter: 請填入您的TAMedia版位ID (可向您的TAMedia窗口詢問)
2017admob step2-3.png



程式整合-Getting Started

1. 請參考AdMob for Android網站提供的方式整合Google Ads SDK
2. 下載 TAMedia Android SDK, 並參考 "開始使用Getting Started" 整合TAMedia Android SDK
3. 加入必要的權限
TWMAd服務需使用 "存取網路狀態"、"讀取約略位置"、"網路" 等權限,請在AndroidManifest.xml檔案當中進行下列權限的宣告。
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- MADP SDK4.0以上版本請移除 -->
<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.VIBRATE" /> <!-- optional -->
4. 加入必要的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>

<!-- 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" />



程式整合-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註冊於mediation的設定頁面上, 如前述章節所述
 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註冊於mediation的設定頁面上, 如前述章節所述
 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 (For Android Studio)
Download Sample Code (For Eclipse)

以下以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
2017admob step5-1.png


回首頁