插頁式獎勵廣告是一種獎勵廣告格式,您可在應用程式自然轉換時自動顯示這類廣告,向使用者提供獎勵。與獎勵廣告不同的是,插頁式獎勵廣告不需要等使用者確定選擇觀看即可放送。
必要條件
- Google Mobile Ads SDK 19.2.0 以上版本。
- 完成入門指南。
導入作業
整合插頁式獎勵廣告的主要步驟如下:
- 載入廣告
- 註冊全螢幕事件回呼
- 處理獎勵回呼
- 顯示廣告
載入廣告
載入廣告時,請使用 RewardedInterstitialAd
類別的靜態 load()
方法。載入方法需要 Context、廣告單元 ID、AdManagerAdRequest
物件,以及在廣告載入成功或失敗時收到通知的 RewardedInterstitialAdLoadCallback
。載入的 RewardedInterstitialAd
物件會以參數形式提供給 onRewardedInterstitialAdLoaded()
回呼。
以下範例說明如何在 MainActivity
中載入 RewardedInterstitialAd
。
Java
Kotlin
將 AD_UNIT_ID 替換為廣告單元 ID。
註冊回呼
如要接收簡報事件的通知,您必須將 FullScreenContentCallback
物件傳遞至廣告的設定器。FullScreenContentCallback
物件會處理廣告成功或失敗顯示時,以及廣告遭到關閉時的回呼。下列程式碼顯示如何在 RewardedInterstitialAdLoadCallback
中設定匿名 FullScreenContentCallback
物件:
Java
rewardedInterstitialAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d(TAG, "The ad was dismissed.");
// Make sure to set your reference to null so you don't
// show it a second time.
rewardedInterstitialAd = null;
if (googleMobileAdsConsentManager.canRequestAds()) {
loadRewardedInterstitialAd();
}
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d(TAG, "The ad failed to show.");
// Make sure to set your reference to null so you don't
// show it a second time.
rewardedInterstitialAd = null;
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "The ad was shown.");
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "The ad recorded an impression.");
}
@Override
public void onAdClicked() {
// Called when ad is clicked.
Log.d(TAG, "The ad was clicked.");
}
});
rewardedInterstitialAd.show(
MainActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.d(TAG, "The user earned the reward.");
// Handle the reward.
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
}
private void initializeMobileAdsSdk() {
if (isMobileAdsInitializeCalled.getAndSet(true)) {
return;
}
// Set your test devices.
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder()
.setTestDeviceIds(Arrays.asList(TEST_DEVICE_HASHED_ID))
.build());
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
// Load an ad on the main thread.
runOnUiThread(() -> loadRewardedInterstitialAd());
})
.start();
}
}
Kotlin
rewardedInterstitialAd?.fullScreenContentCallback =
object : FullScreenContentCallback() {
override fun onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d(TAG, "Ad was dismissed.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedInterstitialAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when fullscreen content failed to show.
Log.d(TAG, "Ad failed to show.")
// Don't forget to set the ad reference to null so you
// don't show the ad a second time.
rewardedInterstitialAd = null
}
override fun onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdClicked() {
// Called when an ad is clicked.
Log.d(TAG, "Ad was clicked.")
}
}
顯示廣告
顯示獎勵插頁式廣告時,您可以使用 OnUserEarnedRewardListener
物件處理獎勵事件。
Java
rewardedInterstitialAd.show(
MainActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.d(TAG, "The user earned the reward.");
// Handle the reward.
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
Kotlin
rewardedInterstitialAd?.show(this) { rewardItem ->
Log.d(TAG, "User earned the reward.")
// Handle the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
}
GitHub 上的範例
後續步驟
請參閱下列主題: