前提条件
- Google 移动广告 SDK 19.7.0 或更高版本。
- 完成入门指南。
始终使用测试广告进行测试
在构建和测试应用时,请务必使用测试广告 实际投放的广告。否则,可能会导致您的账号被暂停。
要加载测试广告,最简便的方法就是使用我们的专用测试广告单元 ID Android 激励广告:
ca-app-pub-3940256099942544/5224354917
该测试广告单元 ID 已经过专门配置,可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。只需制作 请务必在发布应用前用您自己的广告单元 ID 替换该测试广告单元。
如需详细了解移动广告 SDK 的测试广告如何运作,请参阅测试 广告。
加载激励广告对象
对激励广告调用静态 load()
方法可加载激励广告
RewardedAd
类并传入 RewardedAdLoadCallback
。这通常是
在 Activity
的 onCreate()
方法中完成。
请注意,与其他广告格式加载回调一样,RewardedAdLoadCallback
利用 LoadAdError
提供保真度更高的错误详情。
Java
import com.google.android.gms.ads.rewarded.RewardedAd;
public class MainActivity extends Activity {
private RewardedAd rewardedAd;
private final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.toString());
rewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd ad) {
rewardedAd = ad;
Log.d(TAG, "Ad was loaded.");
}
});
}
}
Kotlin
class MainActivity : AppCompatActivity() {
private var rewardedAd: RewardedAd? = null
private final var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var adRequest = AdRequest.Builder().build()
RewardedAd.load(this,"ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedAd = null
}
override fun onAdLoaded(ad: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
rewardedAd = ad
}
})
}
}
设置 FullScreenContentCallback
FullScreenContentCallback
会处理与显示您的
RewardedAd
。在显示 RewardedAd
之前,请务必设置回调
如下所示:
Java
rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
rewardedAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedAd = null;
}
@Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
Kotlin
rewardedAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}
override fun onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.")
rewardedAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
rewardedAd = null
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
}
展示广告
在展示激励广告时,您将使用 OnUserEarnedRewardListener
对象
处理奖励事件。
Java
if (rewardedAd != null) {
Activity activityContext = MainActivity.this;
rewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// Handle the reward.
Log.d(TAG, "The user earned the reward.");
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
}
});
} else {
Log.d(TAG, "The rewarded ad wasn't ready yet.");
}
Kotlin
rewardedAd?.let { ad ->
ad.show(this, OnUserEarnedRewardListener { rewardItem ->
// Handle the reward.
val rewardAmount = rewardItem.amount
val rewardType = rewardItem.type
Log.d(TAG, "User earned the reward.")
})
} ?: run {
Log.d(TAG, "The rewarded ad wasn't ready yet.")
}
[可选] 验证服务器端验证 (SSV) 回调
需要在服务器端获取额外数据的应用
验证回调应使用
自定义数据功能。在激励广告上设置的任何字符串值
对象会传递给 SSV 回调的 custom_data
查询参数。如果拒绝
自定义数据值后,custom_data
查询参数值不会
。
以下代码示例演示了如何为激励广告设置自定义数据 对象。
Java
RewardedAd.load(MainActivity.this, "ca-app-pub-3940256099942544/5354046379",
new AdRequest.Builder().build(), new RewardedAdLoadCallback() {
@Override
public void onAdLoaded(RewardedAd ad) {
Log.d(TAG, "Ad was loaded.");
rewardedAd = ad;
ServerSideVerificationOptions options = new ServerSideVerificationOptions
.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build();
rewardedAd.setServerSideVerificationOptions(options);
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedAd = null;
}
});
Kotlin
RewardedAd.load(this, "ca-app-pub-3940256099942544/5354046379",
AdRequest.Builder().build(), object : RewardedAdLoadCallback() {
override fun onAdLoaded(ad: RewardedAd) {
Log.d(TAG, "Ad was loaded.")
rewardedInterstitialAd = ad
val options = ServerSideVerificationOptions.Builder()
.setCustomData("SAMPLE_CUSTOM_DATA_STRING")
.build()
rewardedAd.setServerSideVerificationOptions(options)
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedAd = null
}
})
如果您要设置自定义奖励字符串,则必须在显示 。
常见问题解答
- 初始化调用是否会超时?
- 10 秒后,Google 移动广告 SDK 调用
OnInitializationCompleteListener
,即使某个中介广告联盟仍未 已完成初始化。 - 在获得初始化回调时,如果某些中介广告联盟尚未就绪,该怎么办?
我们建议在
OnInitializationCompleteListener
。即使中介广告联盟尚未就绪 Google 移动广告 SDK 仍会向该广告联盟请求投放广告。因此,如果 中介广告联盟在超时后完成初始化,它仍然可以 处理该会话中未来的广告请求您可以继续在整个过程中轮询所有适配器的初始化状态 您的应用会话中。
MobileAds.getInitializationStatus()
- 如何找出特定中介广告联盟未准备就绪的原因?
AdapterStatus.getDescription()
描述了适配器未准备好 服务广告请求。- 系统是否始终会在调用
onAdDismissedFullScreenContent()
回调之前调用onUserEarnedReward()
回调? 对于 Google Ads,所有
onUserEarnedReward()
调用都发生在onAdDismissedFullScreenContent()
。对于通过 中介、第三方广告 网络 SDK 的实现会决定回调顺序。对于为单次关闭回调提供奖励信息的广告联盟 SDK,中介适配器会在调用onAdDismissedFullScreenContent()
之前调用onUserEarnedReward()
。
GitHub 上的示例
后续步骤
请浏览以下主题: