โฆษณาคั่นระหว่างหน้าคือโฆษณาแบบเต็มหน้าจอที่แสดงโดยครอบคลุมทั้งอินเทอร์เฟซของแอป คุณแสดงโฆษณาเหล่านี้ที่จุดเปลี่ยนตามปกติในแอป เช่น ระหว่างกิจกรรมหรือในช่วงหยุดชั่วคราวระหว่างด่านในเกม
เมื่อแอปแสดงโฆษณาคั่นระหว่างหน้า ผู้ใช้สามารถแตะโฆษณาและดำเนินการต่อ หรือปิดโฆษณาและกลับไปที่แอปก็ได้
คู่มือนี้อธิบายวิธีผสานรวมโฆษณาคั่นระหว่างหน้าลงในแอป Android
ก่อนเริ่มต้น
โปรดทำสิ่งต่อไปนี้ก่อนดำเนินการต่อ
- ตั้งค่า GMA Next-Gen SDK
- ใช้รหัสหน่วยโฆษณาคั่นระหว่างหน้าทดสอบ
ca-app-pub-3940256099942544/1033173712- เมื่อสร้างและทดสอบแอป โปรดตรวจสอบว่าคุณใช้โฆษณาทดสอบ ไม่ใช่โฆษณาจริงที่ใช้งานจริง หากคุณไม่ใช้รหัสหน่วยโฆษณาทดสอบ Google อาจระงับบัญชีของคุณ
- ก่อนเผยแพร่แอป ให้แทนที่รหัสนี้ด้วยรหัสหน่วยโฆษณา
- ดูรายละเอียดเกี่ยวกับโฆษณาทดสอบของ GMA Next-Gen SDK ได้ที่เปิดใช้โฆษณาทดสอบ
โหลดโฆษณา
ตัวอย่างต่อไปนี้จะโหลดโฆษณาเดียว
Kotlin
// Load ads after you initialize MobileAds. InterstitialAd.load( AdRequest.Builder(adUnitId).build(), object : AdLoadCallback<InterstitialAd> { override fun onAdLoaded(ad: InterstitialAd) { // Interstitial ad loaded. interstitialAd = ad } override fun onAdFailedToLoad(adError: LoadAdError) { // Interstitial ad failed to load. Log.e(TAG, "Interstitial ad failed to load: ${adError.message}") interstitialAd = null } }, )
Java
// Load ads after you initialize MobileAds. InterstitialAd.load( new AdRequest.Builder(adUnitId).build(), new AdLoadCallback<InterstitialAd>() { @Override public void onAdLoaded(@NonNull InterstitialAd ad) { // Interstitial ad loaded. interstitialAd = ad; } @Override public void onAdFailedToLoad(@NonNull LoadAdError adError) { // Interstitial ad failed to load. Log.e(TAG, "Interstitial ad failed to load: " + adError.getMessage()); interstitialAd = null; } });
เมื่อทดสอบ ให้ใช้รหัสหน่วยโฆษณาทดสอบต่อไปนี้
ca-app-pub-3940256099942544/1033173712
แสดงโฆษณา
หากต้องการแสดงโฆษณาคั่นระหว่างหน้า ให้เรียกใช้เมธอดshow
ตัวอย่างต่อไปนี้แสดงโฆษณา
Kotlin
private fun showAd(interstitialAd: InterstitialAd, activity: Activity) { // Show the ad. interstitialAd.show(activity) }
Java
private void showAd(InterstitialAd interstitialAd, Activity activity) { // Show the ad. interstitialAd.show(activity); }
รอเหตุการณ์โฆษณา
ก่อนแสดงโฆษณา ให้รอเหตุการณ์ในวงจรของโฆษณา ตัวอย่างต่อไปนี้จะบันทึก Callback สำหรับเหตุการณ์โฆษณา
Kotlin
// Listen for ad events. val ad = interstitialAd if (ad == null) { Log.e(TAG, "Interstitial ad is not ready yet.") return } ad.adEventCallback = object : InterstitialAdEventCallback { override fun onAdShowedFullScreenContent() { // Interstitial ad did show. } override fun onAdDismissedFullScreenContent() { // Interstitial ad did dismiss. interstitialAd = null } override fun onAdFailedToShowFullScreenContent( fullScreenContentError: FullScreenContentError ) { // Interstitial ad failed to show. Log.e(TAG, "Interstitial ad failed to show: ${fullScreenContentError.message}") } override fun onAdImpression() { // Interstitial ad did record an impression. } override fun onAdClicked() { // Interstitial ad did record a click. } }
Java
// Listen for ad events. if (interstitialAd == null) { Log.e(TAG, "Interstitial ad is not ready yet."); return; } interstitialAd.setAdEventCallback( new InterstitialAdEventCallback() { @Override public void onAdShowedFullScreenContent() { // Interstitial ad did show. } @Override public void onAdDismissedFullScreenContent() { // Interstitial ad did dismiss. interstitialAd = null; } @Override public void onAdFailedToShowFullScreenContent( @NonNull FullScreenContentError fullScreenContentError) { // Interstitial ad failed to show. Log.e(TAG, "Interstitial ad failed to show: " + fullScreenContentError.getMessage()); } @Override public void onAdImpression() { // Interstitial ad did record an impression. } @Override public void onAdClicked() { // Interstitial ad did record a click. } });