插頁式獎勵廣告會自動在應用程式的自然轉換點顯示,是用來向使用者發放獎勵的廣告格式。與獎勵廣告不同的是,插頁式獎勵廣告不需等使用者選擇觀看即可放送。
必要條件
- 完成入門指南的步驟。
一律使用測試廣告進行測試
建構及測試應用程式時,請務必使用測試廣告,而非實際的正式廣告。否則帳戶可能會遭到停權。
如要載入測試廣告,最簡單的方法是使用 Android 插頁式獎勵廣告專用的測試廣告單元 ID:
ca-app-pub-3940256099942544/5354046379
這種 ID 經特別設定,會針對每個請求傳回測試廣告。您可在編寫程式碼、測試及偵錯時,在自家應用程式中使用測試用 ID。但別忘了在發布應用程式前,將這類 ID 替換為自己的廣告單元 ID。
如要進一步瞭解 Google Mobile Ads SDK (Beta 版) 測試廣告的運作方式,請參閱「測試廣告」。
載入廣告
如要載入廣告,Google Mobile Ads SDK (Beta 版) 提供下列選項:
使用單一廣告載入 API 載入。
使用廣告預先載入 API 載入,不必手動載入及快取廣告。
使用單一廣告載入 API 載入
以下範例說明如何載入單一廣告:
Kotlin
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAd
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAdEventCallback
import com.google.android.libraries.ads.mobile.sdk.MobileAds
class RewardedInterstitialActivity : Activity() {
private var rewardedInterstitialAd: RewardedInterstitialAd? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Load ads after you initialize Google Mobile Ads SDK (beta).
RewardedInterstitialAd.load(
AdRequest.Builder(AD_UNIT_ID).build(),
object : AdLoadCallback<RewardedInterstitialAd> {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
// Rewarded interstitial ad loaded.
rewardedInterstitialAd = ad
}
override fun onAdFailedToLoad(adError: LoadAdError) {
// Rewarded interstitial ad failed to load.
rewardedInterstitialAd = null
}
},
)
}
companion object {
// Sample rewarded interstitial ad unit ID.
const val AD_UNIT_ID = "ca-app-pub-3940256099942544/5354046379"
}
}
Java
import com.google.android.libraries.ads.mobile.sdk.common.AdLoadCallback;
import com.google.android.libraries.ads.mobile.sdk.common.AdRequest;
import com.google.android.libraries.ads.mobile.sdk.common.FullScreenContentError;
import com.google.android.libraries.ads.mobile.sdk.common.LoadAdError;
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.libraries.ads.mobile.sdk.rewardedinterstitial.RewardedInterstitialAdEventCallback;
import com.google.android.libraries.ads.mobile.sdk.MobileAds;
class RewardedActivity extends Activity {
// Sample rewarded interstitial ad unit ID.
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5354046379";
private RewardedInterstitialAd rewardedInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load ads after you initialize Google Mobile Ads SDK (beta).
RewardedInterstitialAd.load(
new AdRequest.Builder(AD_UNIT_ID).build(),
new AdLoadCallback<RewardedInterstitialAd>() {
@Override
public void onAdLoaded(@NonNull RewardedInterstitialAd rewardedInterstitialAd) {
// Rewarded interstitial ad loaded.
AdLoadCallback.super.onAdLoaded(rewardedInterstitialAd);
RewardedActivity.this.rewardedInterstitialAd = rewardedInterstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError adError) {
// Rewarded interstitial ad failed to load.
AdLoadCallback.super.onAdFailedToLoad(adError);
rewardedInterstitialAd = null;
}
}
);
}
}
使用廣告預先載入 API 載入
如要開始預先載入,請執行下列操作:
使用廣告請求初始化預先載入設定。
使用廣告單元 ID 和預先載入設定,啟動插頁式獎勵廣告的預先載入器:
Kotlin
private fun startPreloading(adUnitId: String) {
val adRequest = AdRequest.Builder(adUnitId).build()
val preloadConfig = PreloadConfiguration(adRequest)
RewardedInterstitialAdPreloader.start(adUnitId, preloadConfig)
}
Java
private void startPreloading(String adUnitId) {
AdRequest adRequest = new AdRequest.Builder(adUnitId).build();
PreloadConfiguration preloadConfig = new PreloadConfiguration(adRequest);
RewardedInterstitialAdPreloader.start(adUnitId, preloadConfig);
}
準備好顯示廣告時,請從預先載入器輪詢廣告:
Kotlin
// Polling returns the next available ad and loads another ad in the background.
val ad = RewardedInterstitialAdPreloader.pollAd(adUnitId)
Java
// Polling returns the next available ad and loads another ad in the background.
final RewardedInterstitialAd ad = RewardedInterstitialAdPreloader.pollAd(adUnitId);
設定 RewardedInterstitialAdEventCallback
RewardedInterstitialAdEventCallback 會處理與顯示 RewardedInterstitialAd 相關的事件,顯示插頁式獎勵廣告前,請務必設定回呼:
Kotlin
// Listen for ad events.
rewardedInterstitialAd?.adEventCallback =
object : RewardedInterstitialAdEventCallback {
override fun onAdShowedFullScreenContent() {
// Rewarded interstitial ad did show.
}
override fun onAdDismissedFullScreenContent() {
// Rewarded interstitial ad did dismiss.
rewardedInterstitialAd = null
}
override fun onAdFailedToShowFullScreenContent(
fullScreenContentError: FullScreenContentError
) {
// Rewarded interstitial ad failed to show.
rewardedInterstitialAd = null
}
override fun onAdImpression() {
// Rewarded interstitial ad did record an impression.
}
override fun onAdClicked() {
// Rewarded interstitial ad did record a click.
}
}
Java
// Listen for ad events.
rewardedInterstitialAd.setAdEventCallback(
new RewardedInterstitialAdEventCallback() {
@Override
public void onAdShowedFullScreenContent() {
// Rewarded interstitial ad did show.
RewardedInterstitialAdEventCallback.super.onAdShowedFullScreenContent();
}
@Override
public void onAdDismissedFullScreenContent() {
// Rewarded interstitial ad did dismiss.
RewardedInterstitialAdEventCallback.super.onAdDismissedFullScreenContent();
rewardedInterstitialAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(
@NonNull FullScreenContentError fullScreenContentError) {
// Rewarded interstitial ad failed to show.
RewardedInterstitialAdEventCallback.super.onAdFailedToShowFullScreenContent(
fullScreenContentError);
rewardedInterstitialAd = null;
}
@Override
public void onAdImpression() {
// Rewarded interstitial ad did record an impression.
RewardedInterstitialAdEventCallback.super.onAdImpression();
}
@Override
public void onAdClicked() {
// Rewarded interstitial ad did record a click.
RewardedInterstitialAdEventCallback.super.onAdClicked();
}
}
);
顯示廣告
請使用 show() 方法顯示插頁式獎勵廣告,並透過 OnUserEarnedRewardListener 物件來處理獎勵事件。
Kotlin
// Show the ad.
rewardedInterstitialAd?.show(
this@RewardedActivity,
object : OnUserEarnedRewardListener {
override fun onUserEarnedReward(rewardItem: RewardItem) {
// User earned the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
}
},
)
Java
// Show the ad.
rewardedInterstitialAd.show(
RewardedActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// User earned the reward.
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
範例
請下載並執行範例應用程式,瞭解如何使用 Google Mobile Ads SDK (Beta 版)。