SDK導入說明
使用AdMob中介服務前,需先下載Android AdMob檔案包,即進行解壓縮程序,包含了一個jar檔
及一個java檔
,並完成以下前置步驟:
A.在本專案中
先加入 MFAD-*.*.*.jar
B.加入Google Play Service
C.設定AndroidManifest.xml
D.於AdMob平台設定
E.ClickForce AD 設定
基本設定
引入SDK
在 Android Studio 專案中加入 MFAD-*.*.*.jar
加入Google Play Service
在 Android Studio 專案層級的 build.gradle
中,加入Google Mobile Ads SDK。並請加入maven指令。
. . .. ... versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } allprojects { repositories { google() mavenCentral() } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'com.google.android.gms:play-services-ads:21.5.0' testCompile 'junit:junit:4.12' implementation files('libs/MFAD-2.0.4.jar') }
Note: 可查看 Setting Up Google Play Services,瞭解如何設定 Google Play Services SDK。 建議使用最新版Google Play Services,以支援所有功能。
GoogleMobileAdsSdk 設定
設定AndroidManifest.xml
INTERNET
必要。用來存取網路,以發出廣告請求。
ACCESS_WIFI_STATE
必要。允許訪問Wi-Fi網路狀態信息。
ACCESS_NETWORK_STATE
必要。用來在發出廣告請求前,先行檢查是否有可用的網路連結。
READ_PHONE_STATE
必要。讀取手機狀態,並用來在發出電話請求前,先行檢查是否有可以進行通話。
ACCESS_COARSE_LOCATION
必要。允許訪問粗略性位置,以取得地理相關的廣告。
加入至 AndroidManifest.xml 中
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
加進參考 Google Play 服務版本的 <meta-data> 代碼,Android 可藉此瞭解應用程式預期要用的服務版本。
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> </application>
由於Google Play Services 17.0.0 開始需在 <meta-data> 代碼裡加上 APPLICATION_ID,如下圖第二段 meat 代碼內。
<application android:allowBackup="true " android:icon="@mipmap/ic_launcher " android:label="@string/app_name " android:roundIcon="@mipmap/ic_launcher_round " android:supportsRtl="true " android:theme="@style/AppTheme "> <activity android:name=".MainActivity "> <intent-filter> <action android:name="android.intent.action.MAIN " /> <category android:name="android.intent.category.LAUNCHER " /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy" /> </application>
Note: 可參考 Google Update your AndroidManifest.xml,瞭解如何在您的AndroidManifest設定。
ACCESS_FINE_LOCATION
選用。允許訪問精準性定位,以提供更精準的定位取得地理相關的廣告。
WAKE_LOCK
選用。
WRITE_EXTERNAL_STORAGE
選用。允許寫入外部儲存裝置,與其他權限互相搭配,以取得更多樣性互動的廣告。
CALL_PHONE
選用。用來發出打電話請求,提供使用者更多的廣告資訊。
加入至 AndroidManifest.xml 中
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.CALL_PHONE" />
AdMob 設定
前往AdMob網站設定應用程式。新增完應用程式後,並記下你的廣告單元編號再從中介服務新增廣告來源,如下圖:
首先建立廣告單元:
選擇廣告單元廣告格式:
編輯廣告單元名稱:
建立完成廣告單元:
建立中介服務群:
建立廣告格式跟平台:
建立地區跟建立廣告單元:
廣告單元設置:
新增自訂事件:
自訂事件設定:
設定自訂廣告單元: (Class Name and Parameter)
-
Class Name
- 填入 PackageName.ClassName
(PackageName為 com.xxx.xxxx)
(ClassName為SDK.zip裡提供的AdMobCustomAdapter
or AdMobIntCustomAD or AdMobNativeCustomAD File Name) -
Parameter
- 填入版位號(域動廣告版位)
完成:
ClickForce AD 設定
設定 AdView layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.force.click.cfaadmobsample.MainActivity"> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxxs"> </com.google.android.gms.ads.AdView> <RelativeLayout>
將以下兩項加進 XML:
1. 廣告額外使用的命名空間:http://schemas.android.com/apk/res-auto。
2. AdView 的新元素。系統會要求您提供 layout_width 和 layout_height,您可將這兩項皆設為 wrap_content。在 AdView 代碼中,將 adSize 設定為 BANNER,
在ads:adUnitId
中填入剛剛在AdMob申請的廣告單元編號。
宣告及引入
1.依照在AdMob平台上所設定的Class路徑,將Class引入至專案。 (該Class為Android AdMob壓縮包中的java檔)
2.加入程式碼
import ...... import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"); AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } }
取得結果
宣告及引入
1.依照在AdMob平台上所設定的Class路徑,將Class引入至專案。 (該Class為Android AdMob壓縮包中的java檔)
2.加入程式碼
import ...... import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"); InterstitialAd admob_InterstitialAd; = (AdView) findViewById(R.id.adView); admob_InterstitialAd = new InterstitialAd(this); admob_InterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"); admob_InterstitialAd.setAdListener(new AdListener(){ @Override public void onAdFailedToLoad(int errorCode) { Toast.makeText(MainActivity.this, "Error loading custom event interstitial, code " + errorCode, Toast.LENGTH_SHORT).show(); } }); if (admob_InterstitialAd != null && admob_InterstitialAd.isLoaded()){ admob_InterstitialAd.show(); }else { if (!admob_InterstitialAd.isLoading()&&!admob_InterstitialAd.isLoaded()){ AdRequest adRequest = new AdRequest.Builder().build(); DMCustomAD.getA(this); admob_InterstitialAd.loadAd(adRequest); } } } }
取得結果
宣告及引入
1.依照在AdMob平台上所設定的Class路徑,將Class引入至專案。 (該Class為Android AdMob壓縮包中的java檔)
2.加入程式碼
import ...... import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdLoader; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.VideoOptions; import com.google.android.gms.ads.doubleclick.PublisherAdRequest; import com.google.android.gms.ads.formats.MediaView; import com.google.android.gms.ads.formats.NativeAdOptions; import com.google.android.gms.ads.formats.UnifiedNativeAd; import com.google.android.gms.ads.formats.UnifiedNativeAdView; public class MainActivity extends AppCompatActivity { private String Clickforce_ADMOB_UNIT = "ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx"; private UnifiedNativeAd nativeAd; private RelativeLayout relativeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayout = (RelativeLayout)findViewById(R.id.mRelativeLayout); MobileAds.initialize(this); AdLoader.Builder builder = new AdLoader.Builder(this, Clickforce_ADMOB_UNIT); builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() { @Override public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) { if (nativeAd != null){ nativeAd.destroy(); } nativeAd = unifiedNativeAd; UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.ad_unified_official,null); populateUnifiedNativeAd(unifiedNativeAd,adView); } }); VideoOptions videoOptions = new VideoOptions.Builder() .setStartMuted(true) .build(); NativeAdOptions adOptions = new NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build(); builder.withNativeAdOptions(adOptions); AdLoader adLoader = builder.withAdListener(new AdListener(){ @Override public void onAdImpression() { super.onAdImpression(); Log.d("UnifiedNativeAd","onAdImpression "); } @Override public void onAdFailedToLoad(int i) { super.onAdFailedToLoad(i); Log.d("UnifiedNativeAd","AdFailedToLoad "+"Code: "+i); } @Override public void onAdClicked() { super.onAdClicked(); Log.d("UnifiedNativeAd","onAdClicked"); } }).build(); adLoader.loadAd(new PublisherAdRequest.Builder().build()); // adLoader.loadAd(new PublisherAdRequest.Builder().addTestDevice("").build());//set test device ID. } private void populateUnifiedNativeAd(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView){ MediaView mediaView = adView.findViewById(R.id.ad_media); adView.setMediaView(mediaView); adView.setHeadlineView(adView.findViewById(R.id.ad_headline)); adView.setBodyView(adView.findViewById(R.id.ad_body)); adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action)); adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser)); adView.setIconView(adView.findViewById(R.id.ad_app_icon)); //tittle ((TextView)adView.getHeadlineView()).setText(nativeAd.getHeadline()); //content if (nativeAd.getBody() == null){ adView.getBodyView().setVisibility(View.INVISIBLE); }else{ adView.getBodyView().setVisibility(View.VISIBLE); ((TextView) adView.getBodyView()).setText(nativeAd.getBody()); } //advertiser if (nativeAd.getAdvertiser() == null){ adView.getAdvertiserView().setVisibility(View.INVISIBLE); }else{ adView.getAdvertiserView().setVisibility(View.VISIBLE); ((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser()); } //coverImage if (nativeAd.getIcon() == null){ adView.getIconView().setVisibility(View.GONE); }else{ ((ImageView) adView.getIconView()).setImageDrawable(nativeAd.getIcon().getDrawable()); adView.getIconView().setVisibility(View.VISIBLE); } //button if (nativeAd.getCallToAction() == null){ adView.getCallToActionView().setVisibility(View.INVISIBLE); }else { ((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction()); } adView.setNativeAd(nativeAd); relativeLayout.removeAllViews(); relativeLayout.addView(adView); } @Override protected void onDestroy() { super.onDestroy(); nativeAd.destroy(); } }
取得結果
show date