子母畫面 (PiP) 廣告會顯示在浮動視窗中,並置於螢幕內容 (例如文章、動態消息或遊戲畫面) 的頂端。使用者可與應用程式互動,同時看到廣告。如要顯示不會佔用整個螢幕的廣告,請選擇這個格式。
本指南說明如何使用 GMA Next-Gen SDK,在應用程式中要求及顯示子母畫面廣告。
事前準備
繼續操作前,請先執行下列工作:
安裝 GMA Next-Gen SDK
1.4.0以上版本。啟用測試廣告,並使用下列測試廣告單元 ID:
/21775744923/example/picture-in-picture
載入廣告
如要載入PictureInPictureAd,請建立廣告請求並呼叫 load 方法:
Kotlin
Java
將 AD_UNIT_ID 替換為廣告單元 ID。
顯示廣告
如要在畫面上顯示子母畫面廣告,請設定子母畫面選項並呼叫 show 方法。以下範例會將廣告的預設位置和顯示範圍設為畫面:
Kotlin
private fun showPictureInPictureAd(activity: Activity) { // Capture the ad reference saved from the onAdLoaded callback. val ad = pipAd if (ad != null) { val options = PictureInPictureAdOptions.Builder() // Uses the Google Mobile Ads SDK's default screen position. .setPosition(PictureInPictureAdPosition.DEFAULT) // Binds the ad lifecycle to the host screen. .setPresentationScope(PictureInPictureAdPresentationScope.SCREEN) .build() ad.show(activity, options) } else { Log.d(TAG, "No ad to show.") } }
Java
private void showPictureInPictureAd(@NonNull Activity activity) { // Use the ad reference saved from the onAdLoaded callback. if (pipAd != null) { PictureInPictureAdOptions options = new PictureInPictureAdOptions.Builder() // Uses the Google Mobile Ads SDK's default screen position. .setPosition(PictureInPictureAdPosition.DEFAULT) // Binds the ad lifecycle to the host screen. .setPresentationScope(PictureInPictureAdPresentationScope.SCREEN) .build(); pipAd.show(activity, options); } else { Log.d(TAG, "No ad to show."); } }
設定位置
根據預設,GMA Next-Gen SDK 首次顯示時,會在畫面右下角顯示子母畫面廣告,如果先前顯示過,則會顯示在最後已知的位置。如要自訂廣告顯示位置,請在子母畫面選項中設定位置。以下範例會在畫面左上角,將位置設為內容頂端:
Kotlin
private fun createTopLeftPositionOptions(): PictureInPictureAdOptions { return PictureInPictureAdOptions.Builder() // Sets the ad position to the top-left corner of the screen. .setPosition(PictureInPictureAdPosition.TOP_LEFT) .build() }
Java
private PictureInPictureAdOptions createTopLeftPositionOptions() { return new PictureInPictureAdOptions.Builder() // Sets the ad position to the top-left corner of the screen. .setPosition(PictureInPictureAdPosition.TOP_LEFT) .build(); }
如要查看所有職缺,請參閱PictureInPictureAdPosition。
設定簡報範圍
根據預設,GMA Next-Gen SDK 會將子母畫面廣告繫結至目前的宿主畫面。當主機畫面的檢視區塊階層不再位於記憶體中時,GMA Next-Gen SDK 會關閉廣告。如要讓廣告在主機畫面從記憶體中移除後仍可見,請將顯示範圍設為應用程式:
Kotlin
private fun createApplicationScopedOptions(): PictureInPictureAdOptions { return PictureInPictureAdOptions.Builder() // Keeps the ad visible beyond the host screen's lifecycle. .setPresentationScope(PictureInPictureAdPresentationScope.APPLICATION) .build() }
Java
private PictureInPictureAdOptions createApplicationScopedOptions() { return new PictureInPictureAdOptions.Builder() // Keeps the ad visible beyond the host screen's lifecycle. .setPresentationScope(PictureInPictureAdPresentationScope.APPLICATION) .build(); }
詳情請參閱「在不同螢幕上顯示廣告」。
設定廣告事件回呼
如要處理子母畫面廣告生命週期事件,請在顯示廣告前,為廣告設定事件回呼。這個回呼會回報標準事件,例如點擊和曝光。這個回呼也會回報子母畫面專屬事件,例如廣告顯示或隱藏時:
Kotlin
private fun setAdEventCallback(pipAd: PictureInPictureAd) { pipAd.adEventCallback = object : PictureInPictureAdEventCallback { override fun onAdShown() { Log.d(TAG, "Picture-in-Picture ad shown.") } override fun onAdHidden() { Log.d(TAG, "Picture-in-Picture ad hidden.") } override fun onAdImpression() { Log.d(TAG, "Picture-in-Picture ad recorded an impression.") } override fun onAdClicked() { Log.d(TAG, "Picture-in-Picture ad recorded a click.") } override fun onAdShowedFullScreenContent() { Log.d(TAG, "Picture-in-Picture ad showed full screen content.") } override fun onAdDismissedFullScreenContent() { Log.d(TAG, "Picture-in-Picture ad dismissed full screen content.") } override fun onAdFailedToShowFullScreenContent( fullScreenContentError: FullScreenContentError ) { Log.w( TAG, "Picture-in-Picture ad failed to show full screen content: $fullScreenContentError", ) } override fun onAdPaid(value: AdValue) { Log.d(TAG, "Picture-in-Picture ad paid: ${value.valueMicros} ${value.currencyCode}") } } }
Java
private void setAdEventCallback(@NonNull PictureInPictureAd pipAd) { pipAd.setAdEventCallback( new PictureInPictureAdEventCallback() { @Override public void onAdShown() { Log.d(TAG, "Picture-in-Picture ad shown."); } @Override public void onAdHidden() { Log.d(TAG, "Picture-in-Picture ad hidden."); } @Override public void onAdImpression() { Log.d(TAG, "Picture-in-Picture ad recorded an impression."); } @Override public void onAdClicked() { Log.d(TAG, "Picture-in-Picture ad recorded a click."); } @Override public void onAdShowedFullScreenContent() { Log.d(TAG, "Picture-in-Picture ad showed full screen content."); } @Override public void onAdDismissedFullScreenContent() { Log.d(TAG, "Picture-in-Picture ad dismissed full screen content."); } @Override public void onAdFailedToShowFullScreenContent( @NonNull FullScreenContentError fullScreenContentError) { Log.w( TAG, "Picture-in-Picture ad failed to show full screen content: " + fullScreenContentError); } @Override public void onAdPaid(@NonNull AdValue value) { Log.d( TAG, "Picture-in-Picture ad paid: " + value.getValueMicros() + " " + value.getCurrencyCode()); } }); }
隱藏廣告
如要從畫面上移除浮動廣告,請呼叫 hide 方法。這個方法會叫用廣告隱藏事件回呼:
Kotlin
private fun hidePictureInPictureAd() { // Capture the ad reference saved from the onAdLoaded callback. val ad = pipAd if (ad != null) { ad.hide() } else { Log.d(TAG, "No ad to hide.") } }
Java
private void hidePictureInPictureAd() { // Use the ad reference saved from the onAdLoaded callback. if (pipAd != null) { pipAd.hide(); } else { Log.d(TAG, "No ad to hide."); } }
清理廣告資源
為避免記憶體流失,應用程式使用完廣告後,請捨棄對廣告物件的參照。舉例來說,當應用程式不再顯示或與廣告互動時,如果是以畫面為範圍的廣告,當應用程式從記憶體中移除主機畫面時,請捨棄參照。如果是應用程式範圍的廣告,請在使用者瀏覽不同畫面時保留廣告參照,並在使用者關閉廣告時捨棄參照:
Kotlin
private fun cleanUpPictureInPictureAd() { pipAd?.destroy() pipAd = null }
Java
private void cleanUpPictureInPictureAd() { // Use the ad reference saved from the onAdLoaded callback. if (pipAd != null) { pipAd.destroy(); pipAd = null; } }
確保廣告在各種螢幕上都能顯示
如果將顯示範圍設為應用程式,即使應用程式從記憶體中移除主機畫面,子母畫面廣告仍會顯示。使用者離開主機畫面時,如要與廣告互動或關閉廣告,應用程式必須保留子母畫面廣告的存取權。建議您將廣告保留在應用程式層級的單例模式或共用狀態管理工具中,而不是單一畫面的執行個體變數中。
如需在不同螢幕上顯示廣告的範例,請參閱我們的範例應用程式: