插頁式廣告會全螢幕顯示,覆蓋整個應用程式流程的介面。這類廣告通常會在應用程式流程中的自然轉換點顯示,例如活動之間或遊戲關卡之間的暫停時間。 應用程式顯示插頁式廣告時,使用者可選擇輕觸廣告前往到達網頁,或關閉廣告並返回應用程式。
本指南說明如何將插頁式廣告整合至 Android 應用程式。
必要條件
- Google Mobile Ads SDK 19.7.0 以上版本。
- 完成入門指南。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非正式版廣告。否則可能導致帳戶遭到停權。
如要載入測試廣告,最簡單的方法是使用 Android 插頁式廣告的專屬測試廣告單元 ID:
/6499/example/interstitial
我們已特別將每個請求設為傳回測試廣告;編寫程式碼、測試及偵錯時,您可以在自己的應用程式中自由使用這項工具。但請注意,發布應用程式前,請務必先將它換成自己的廣告單元 ID。
如要進一步瞭解 Mobile Ads SDK 測試廣告的運作方式,請參閱「測試廣告」一文。
載入廣告
如要載入插頁式廣告,請呼叫 AdManagerInterstitialAd
靜態 load()
方法,然後傳入 AdManagerInterstitialAdLoadCallback
以接收已載入的廣告或任何可能的錯誤。請注意,如同其他格式載入回呼,AdManagerInterstitialAdLoadCallback
會利用 LoadAdError
提供更精確的錯誤詳細資料。
Java
import com.google.android.gms.ads.admanager.AdManagerInterstitialAd;
import com.google.android.gms.ads.admanager.AdManagerInterstitialAdLoadCallback;
public class MainActivity extends Activity {
private AdManagerInterstitialAd mAdManagerInterstitialAd;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdManagerAdRequest adRequest = new AdManagerAdRequest.Builder().build();
AdManagerInterstitialAd.load(this,"/6499/example/interstitial", adRequest,
new AdManagerInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull AdManagerInterstitialAd interstitialAd) {
// The mAdManagerInterstitialAd reference will be null until
// an ad is loaded.
mAdManagerInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.d(TAG, loadAdError.toString());
mAdManagerInterstitialAd = null;
}
});
}
}
Kotlin
import com.google.android.gms.ads.admanager.AdManagerInterstitialAd;
import com.google.android.gms.ads.admanager.AdManagerInterstitialAdLoadCallback;
class MainActivity : AppCompatActivity() {
private var mAdManagerInterstitialAd: AdManagerInterstitialAd? = null
private final val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var adRequest = AdManagerAdRequest.Builder().build()
AdManagerInterstitialAd.load(this,"/6499/example/interstitial", adRequest, object : AdManagerInterstitialAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
mAdManagerInterstitialAd = null
}
override fun onAdLoaded(interstitialAd: AdManagerInterstitialAd) {
Log.d(TAG, 'Ad was loaded.')
mAdManagerInterstitialAd = interstitialAd
}
})
}
}
設定 FullScreenContentCallback
FullScreenContentCallback
會處理與顯示 InterstitialAd
相關的事件。顯示 InterstitialAd
之前,請務必設定回呼:
Java
mAdManagerInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
mAdManagerInterstitialAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
mAdManagerInterstitialAd = null;
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
Kotlin
mAdManagerInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}
override fun onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
Log.d(TAG, "Ad dismissed fullscreen content.")
mAdManagerInterstitialAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
mAdManagerInterstitialAd = null
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
}
顯示廣告
插頁式廣告應該在應用程式流程中的自然暫停處顯示。例如,在遊戲關卡之間,或是在使用者完成任務後顯示。如要顯示插頁式廣告,請使用 show()
方法。
Java
if (mAdManagerInterstitialAd != null) {
mAdManagerInterstitialAd.show(MyActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
Kotlin
if (mAdManagerInterstitialAd != null) {
mAdManagerInterstitialAd?.show(this)
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.")
}
一些最佳做法
- 請思考插頁式廣告是否為適合您應用程式的廣告類型。
- 插頁式廣告最適合用於包含自然轉換點的應用程式。 應用程式內的工作結束時 (例如分享圖片或完成遊戲關卡),即會產生這類時間點。請務必考慮在應用程式工作流程中的哪個時間點顯示插頁式廣告,以及使用者的可能反應。
- 請記得在顯示插頁式廣告時暫停動作。
- 插頁式廣告有多種類型:文字、圖像、影片等。請務必確保應用程式顯示插頁式廣告時,一併暫停使用部分資源,廣告才能利用資源。舉例來說,當您呼叫顯示插頁式廣告時,請務必暫停應用程式產生的任何音訊輸出。
- 允許充分的載入時間。
- 確保在適當時機顯示插頁式廣告的重要性,您也應該確保使用者不必等待廣告載入。在您想呼叫
show()
之前呼叫load()
即可事先載入廣告,可確保應用程式在顯示時已有完全載入的插頁式廣告。 - 不要讓應用程式廣告氾濫。
- 增加在應用程式中顯示插頁式廣告的頻率也許是提升收益的好方法,但也有可能導致使用者體驗和點閱率下降。請確保使用者不會經常中斷,以免他們無法再使用您的應用程式。
GitHub 上的範例
後續步驟
- 進一步瞭解廣告指定目標。
- 進一步瞭解使用者隱私權。
- 探索最佳化 SDK 初始化和廣告載入 (Beta 版),