Android Integeration with AdMob Mediation 2.0
跳至導覽
跳至搜尋
- AdMob Mediation整合
- 請至https://mediation.admob.com。
- 登入AdMob(可能需要申請一個AdMob帳號或以Google帳號登入),點選Sites & Apps > Ad Network Mediation。
-
- 新增Banner Ad
- 點擊Add Network Mediation Placement新增一個Mediation Placement。
- 輸入Mediation名稱,選擇Platform、Ad Size及Automatic Refresh。若您的Mediation Placement有2個以上的廣告時,AdMob SDK每5分鐘(300秒)會抽取(依聯播網分配比率)下一個Ad Network。Automatic Refresh的設定,是指這5分鐘內,更新同一個Ad Network內容。完成後按Save & Continue。
- 請在這一步直接按最下方continue(或是可以勾選您目前擁有的Ad Network)。
- <呈現mediation設定畫面, 請將本畫面所提供的mediation_id紀錄下來>
- 按下Add Custom Event
- Label:TAMedia (任意自定的名稱)
- Class Name:請填入實作(implements com.google.ads.mediation.customevent.CustomEventBanner)的類別名稱(含完整的package name, 如com.taiwanmobile.pt.adp.mediation.TAMediaBanner)
- Parameter:請填入您的TAMedia Slot Id (可向您的TAMedia窗口詢問)
- 完成後按下Save
- 若有希望數個AdNetwork能均等投放廣告, 請將eCPM欄位填入同樣的數值
- 整合至應用程式
- 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(); } @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 AdMob Activities --> <activity android:name="com.google.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(activity, AdSize.BANNER, "<Mediation ID>"); layout.addView(adView); adView.loadAd(new AdRequest()); }
- 聯播網投放與維護
- 當APP設定好Mediation並且發佈出去後,可直接透過Mediation網頁進行版位管理與聯播網比率調整,此設定會在1小時後生效。