插页式激励广告是一种激励用户的广告格式,采用这种格式时,您可以通过在应用中的自然过渡点自动展示的广告向用户提供奖励。与激励广告不同,用户无需自行选择即可观看插页式激励广告。
前提条件
- Google 移动广告 SDK 19.2.0 或更高版本。
- 通读入门指南。
实现
植入插页式激励广告的主要步骤如下所示:
- 加载广告
- 注册全屏事件回调
- 处理奖励回调
- 展示广告
加载广告
广告的加载是通过针对 RewardedInterstitialAd
类使用静态 load()
方法完成的。该加载方法需要使用上下文、您的广告单元 ID、AdManagerAdRequest
对象和 RewardedInterstitialAdLoadCallback
(以在广告加载成功或失败时收到通知)。已加载的 RewardedInterstitialAd
对象会以 onRewardedInterstitialAdLoaded()
回调中的参数的形式提供。
以下示例展示了如何在 MainActivity
中加载 RewardedInterstitialAd
。
Java
Kotlin
将 AD_UNIT_ID 替换为您的广告单元 ID。
注册回调
要接收有关展示事件的通知,您必须向广告的 setter 方法传递 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 上的示例
后续步骤
探索以下主题: