Android Integeration with New AdMob Mediation 2.0

出自TAMedia
於 2015年2月12日 (四) 10:25 由 imported>Wikiuser 所做的修訂
跳至導覽 跳至搜尋
  • 新版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{
 			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 serverParameter, String label,
 			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小時後生效。