准备工作
在继续操作之前,请先完成以下事项:
- 设置 Google Mobile Ads SDK (Legacy)。
- 使用测试激励广告单元 ID
ca-app-pub-3940256099942544/5224354917。- 在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。如果您不使用测试广告单元 ID,Google 可能会暂停您的账号。
- 在发布应用之前,请将此 ID 替换为您的广告单元 ID。
- 如需详细了解 Google Mobile Ads SDK (Legacy) 测试广告,请参阅启用测试广告。
加载激励广告对象
如果要加载激励广告,请调用 RewardedAd 类的静态 load 方法并传入 RewardedAdLoadCallback。这通常是在 Activity 的 onCreate 方法中完成的。请注意,与其他用于加载广告格式的回调函数一样,RewardedAdLoadCallback 也会使用 LoadAdError 提供更精细的错误详情。
Java
Kotlin
请将 AD_UNIT_ID 替换为您的广告单元 ID。
设置 FullScreenContentCallback
FullScreenContentCallback 负责处理与展示 RewardedAd 相关的事件。在展示 RewardedAd 之前,请务必按如下方法设置回调函数:
Java
rewardedAd.setFullScreenContentCallback(
new FullScreenContentCallback() {
@Override
public void 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.
rewardedAd = null;
}
@Override
public void 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.
rewardedAd = null;
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdClicked() {
// Called when an ad is clicked.
Log.d(TAG, "Ad was clicked.");
}
});
Kotlin
rewardedAd?.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.
rewardedAd = 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.
rewardedAd = 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
rewardedAd.show(
MainActivity.this,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.d(TAG, "User earned the reward.");
// Handle the reward.
}
});
Kotlin
rewardedAd?.show(
this,
OnUserEarnedRewardListener { rewardItem ->
Log.d(TAG, "User earned the reward.")
// Handle the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
},
)
对于 Google 投放的广告,Google Mobile Ads SDK (Legacy)会在 onAdDismissedFullScreenContent 方法之前调用 onUserEarnedReward 方法。对于通过 AdMob 中介服务投放的广告,回调顺序取决于第三方广告来源。对于在单个关闭回调中提供奖励信息的广告来源,中介适配器会在调用 onAdDismissedFullScreenContent 方法之前先调用 onUserEarnedReward 方法。如需了解详情,请参阅设置 AdMob 中介。
可选:验证服务器端验证 (SSV) 回调
应用如果在服务器端验证回调中需要使用额外的数据,则应使用激励广告的自定义数据功能。在激励广告对象中设置的任何字符串值都将传递给 SSV 回调的 custom_data 查询参数。如果未设置自定义数据值,custom_data 查询参数值不会出现在 SSV 回调中。
以下代码示例演示了在请求广告之前如何在激励广告对象中设置自定义数据。
Java
Kotlin
请将 SAMPLE_CUSTOM_DATA_STRING 替换为自定义数据。
如果您要设置自定义奖励字符串,则必须在展示广告之前设置。
GitHub 示例
后续步骤
探索以下主题: