插页式广告属于全屏广告,会覆盖宿主应用的整个界面,通常展示在应用流程的自然过渡点上,例如,活动之间的切换处或游戏关卡之间的暂停时段中。当应用展示插页式广告时,用户既可以选择点按该广告,进而访问其目标网址,也可以将其关闭,并返回应用。
本指南介绍了如何将插页式广告植入到 Android 应用中。
前提条件
- Google 移动广告 SDK 19.7.0 或更高版本。
- 通读入门指南。
务必用测试广告进行测试
在构建和测试应用时,请确保使用的是测试广告,而不是实际投放的广告。否则,可能会导致您的账号被中止。
对于 Android 插页式广告,加载测试广告最简便的方法就是使用下面的专用测试广告单元 ID:
/21775744923/example/interstitial
该测试广告单元 ID 已经过专门配置,可确保每个请求返回的都是测试广告。您可以在自己应用的编码、测试和调试过程中随意使用该测试广告单元 ID。需要注意的一点是,请务必在发布应用前用您的广告单元 ID 替换该测试广告单元 ID。
如需详细了解 Google 移动广告 SDK 测试广告如何运作,请参阅测试广告。
加载广告
要加载插页式广告,请调用 AdManagerInterstitialAd
静态 load()
方法并传入 AdManagerInterstitialAdLoadCallback
,以接收加载的广告或任何可能的错误。请注意,与其他广告格式加载回调一样,AdManagerInterstitialAdLoadCallback
会利用 LoadAdError
提供较高保真度的错误详情。
Java
AdManagerInterstitialAd.load(
this,
AD_UNIT_ID,
new AdManagerAdRequest.Builder().build(),
new AdManagerInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull AdManagerInterstitialAd interstitialAd) {
Log.d(TAG, "Ad was loaded.");
MyActivity.this.interstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.d(TAG, loadAdError.getMessage());
interstitialAd = null;
}
});
Kotlin
AdManagerInterstitialAd.load(
this,
AD_UNIT_ID,
AdManagerAdRequest.Builder().build(),
object : AdManagerInterstitialAdLoadCallback() {
override fun onAdLoaded(interstitialAd: AdManagerInterstitialAd) {
Log.d(TAG, "Ad was loaded.")
this@MyActivity.interstitialAd = interstitialAd
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError.message)
interstitialAd = null
}
},
)
设置 FullScreenContentCallback
FullScreenContentCallback
处理与展示 InterstitialAd
相关的事件。在展示 InterstitialAd
之前,请务必按如下方式设置回调:
Java
interstitialAd.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.
MyActivity.this.interstitialAd = null;
}
@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.
MyActivity.this.interstitialAd = 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.");
}
});
Kotlin
interstitialAd?.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.
interstitialAd = 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.
interstitialAd = 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 ad is clicked.
Log.d(TAG, "Ad was clicked.")
}
}
展示广告
插页式广告应在应用流程的自然停顿期间进行展示,例如游戏的不同关卡之间或用户完成一项任务之后,都是非常不错的展示时机。如需展示插页式广告,请使用 show()
方法。
Java
if (interstitialAd != null) {
interstitialAd.show(this);
} else {
Log.d(TAG, "The interstitial ad is still loading.");
}
Kotlin
interstitialAd?.show(this)
一些最佳做法
- 考虑插页式广告这种广告类型是否适合您的应用。
- 在具有自然过渡点的应用中,插页式广告的效果最好。此类过渡点通常存在于应用内的任务结束时,例如分享完图片或完成一个游戏关卡时。请务必考虑在应用流程的哪些时间点展示插页式广告,以及用户可能会以什么方式响应。
- 务必在展示插页式广告时暂停操作。
- 插页式广告类型多样,包括文字广告、图片广告和视频广告等。确保应用在展示插页式广告时,也会暂停使用某些资源,以便供广告使用,这一点十分重要。例如,当您发出展示插页式广告的调用后,请务必暂停应用产生的所有音频输出。
- 留出充足的加载时间。
- 确保在恰当的时间展示插页式广告十分重要,同样,确保用户无需等待广告加载也十分重要。在您打算调用
show()
前,请事先通过调用load()
加载广告,这可确保应用在广告展示时间到来前完全加载插页式广告。 - 不要向用户展示太多广告。
- 虽然提高插页式广告在应用中的展示频次似乎是实现增收的好方法,但这么做会影响用户体验,还会降低点击率。应确保用户不会频繁受到广告打扰,使他们可以充分享受到使用应用的乐趣。
GitHub 上的示例
后续步骤
- 详细了解广告定位。
- 详细了解用户隐私。
- 探索优化的 SDK 初始化和广告加载(Beta 版)。