前提条件
- 使用 Google Mobile Ads SDK 19.7.0 或更高版本。
- 完成 Google Mobile Ads SDK 设置。
务必用测试广告进行测试
在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的账号被暂停。
对于 Android 激励广告,加载测试广告最简便的方法就是使用下面的专用测试广告单元 ID:
ca-app-pub-3940256099942544/5224354917
该测试广告单元 ID 已经过专门配置,可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。需要注意的一点是,请务必在发布应用前用您的广告单元 ID 替换该测试广告单元 ID。
如需详细了解 Google Mobile Ads SDK 测试广告,请参阅启用测试广告。
加载激励广告对象
如果要加载激励广告,请调用 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
},
)
[可选] 验证服务器端验证 (SSV) 回调
应用如果在服务器端验证回调中需要使用额外的数据,则应使用激励广告的自定义数据功能。在激励广告对象中设置的任何字符串值都将传递给 SSV 回调的 custom_data 查询参数。如果未设置自定义数据值,custom_data 查询参数值不会出现在 SSV 回调中。
以下代码示例演示了在请求广告之前如何在激励广告对象中设置自定义数据。
Java
Kotlin
请将 SAMPLE_CUSTOM_DATA_STRING 替换为您的自定义数据。
如果您要设置自定义奖励字符串,则必须在展示广告之前设置。
常见问题解答
- 初始化调用有超时限制吗?
- 10 秒后,即使中介广告联盟仍未完成初始化,Google Mobile Ads SDK 还是会调用
OnInitializationCompleteListener。 - 如果在收到初始化回调时,某些中介广告联盟尚未就绪,该怎么办?
我们建议您在
OnInitializationCompleteListener回调中加载广告。即使中介广告联盟尚未就绪,Google Mobile Ads SDK 仍会向该广告联盟请求广告。因此,如果中介广告联盟在超时后完成初始化,它仍然可以满足该会话中后续的广告请求。在整个应用会话期间,您随时可以调用
MobileAds.getInitializationStatus()来持续轮询所有适配器的初始化状态。- 如何查明特定中介广告联盟尚未就绪的原因?
AdapterStatus.getDescription()会说明适配器为何尚未就绪,无法处理广告请求。onUserEarnedReward()回调是否始终在onAdDismissedFullScreenContent()回调之前触发?对于 Google Ads,所有
onUserEarnedReward()调用都发生在onAdDismissedFullScreenContent()调用之前。对于通过中介投放的广告,回调顺序取决于第三方广告联盟 SDK 的实现情况。对于在单个关闭回调中提供奖励信息的广告联盟 SDK,中介适配器会在调用onAdDismissedFullScreenContent()之前先调用onUserEarnedReward()。
GitHub 示例
后续步骤
探索以下主题: