โฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลเป็นประเภท
ของรูปแบบโฆษณาที่ให้สิ่งจูงใจที่ช่วยให้คุณนำเสนอรางวัลสำหรับโฆษณาที่ปรากฏ
โดยอัตโนมัติระหว่างจุดเปลี่ยนหน้าตามปกติของแอป ผู้ใช้ไม่จําเป็นต้องเลือกดูโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัล ซึ่งต่างจากโฆษณาที่มีการให้รางวัล
ข้อกำหนดเบื้องต้น
การใช้งาน
ขั้นตอนหลักในการผสานรวมโฆษณาคั่นระหว่างหน้าที่มีการให้รางวัลมีดังนี้
- โหลดโฆษณา
- ลงทะเบียนเพื่อรับการเรียกกลับของเหตุการณ์แบบเต็มหน้าจอ
- จัดการการเรียกกลับของรางวัล
- แสดงโฆษณา
โหลดโฆษณา
การโหลดโฆษณาทำได้โดยใช้เมธอด load แบบคงที่ในคลาส RewardedInterstitialAd เมธอดโหลดต้องใช้บริบท รหัสหน่วยโฆษณา ออบเจ็กต์ AdManagerAdRequest และ RewardedInterstitialAdLoadCallback เพื่อรับการแจ้งเตือนเมื่อโหลดโฆษณาสำเร็จหรือไม่สำเร็จ ระบบจะส่งออบเจ็กต์ RewardedInterstitialAd ที่โหลดแล้วเป็นพารามิเตอร์ใน
การเรียกกลับ onRewardedInterstitialAdLoaded
ตัวอย่างต่อไปนี้แสดงวิธีโหลด RewardedInterstitialAd ใน MainActivity
Java
RewardedInterstitialAd.load( MainActivity.this, AD_UNIT_ID, new AdManagerAdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() { @Override public void onAdLoaded(RewardedInterstitialAd ad) { Log.d(TAG, "onAdLoaded"); rewardedInterstitialAd = ad; isLoadingAds = false; } @Override public void onAdFailedToLoad(LoadAdError loadAdError) { Log.d(TAG, "onAdFailedToLoad: " + loadAdError.getMessage()); rewardedInterstitialAd = null; isLoadingAds = false; } });Kotlin
RewardedInterstitialAd.load( this, AD_UNIT_ID, AdManagerAdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() { override fun onAdLoaded(rewardedAd: RewardedInterstitialAd) { Log.d(TAG, "Ad was loaded.") rewardedInterstitialAd = rewardedAd isLoadingAds = false } override fun onAdFailedToLoad(adError: LoadAdError) { Log.d(TAG, "onAdFailedToLoad: ${adError.message}") rewardedInterstitialAd = null isLoadingAds = false } }, )แทนที่ AD_UNIT_ID ด้วยรหัสหน่วยโฆษณา
ลงทะเบียนเพื่อรับการเรียกกลับ
หากต้องการรับการแจ้งเตือนสำหรับเหตุการณ์การนำเสนอ คุณต้องส่งออบเจ็กต์ FullScreenContentCallback ไปยัง Setter ในโฆษณา ออบเจ็กต์ FullScreenContentCallback จะจัดการการเรียกกลับเมื่อโฆษณาแสดงสำเร็จหรือไม่สำเร็จ และเมื่อมีการปิดโฆษณา โค้ดต่อไปนี้แสดงวิธีตั้งค่าออบเจ็กต์ FullScreenContentCallback แบบไม่ระบุชื่อภายใน RewardedInterstitialAdLoadCallback
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
ขั้นตอนถัดไป
สำรวจหัวข้อต่อไปนี้