針對 Android TV 進行最佳化調整
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用 Android 版 IMA SDK 時,我們建議您採用幾項最佳做法,確保應用程式可在 Android TV 上正常運作。
請先熟悉開發 Android TV 應用程式的相關資訊。具體來說,請確認您的活動已針對電視進行設定,如入門指南所述。您也應處理電視導覽,確保使用者可在 Android TV 上順利瀏覽應用程式。
處理可略過的廣告
SDK 會針對類似電視的裝置,最佳化可略過的格式,例如移除「瞭解詳情」按鈕的互動功能。根據預設,SDK 會在可略過廣告時將焦點設為略過按鈕,以便在 Android TV 上略過廣告。因此,您不需要額外進行設定,即可支援可略過廣告。
您可以呼叫 AdsRenderingSettings.setFocusSkipButtonWhenAvailable()
來設定這項功能。
如要進一步瞭解支援的廣告類型,請參閱相容性矩陣。
處理 VAST 圖示備用圖片
IMA SDK 會偵測、算繪及處理使用者與 VAST 圖示備用圖片的互動情形。應用程式應監聽 ICON_TAPPED
和 ICON_FALLBACK_IMAGE_CLOSED
事件,以便處理使用「為什麼會顯示這則廣告」(WTA) 的廣告播放。
新增布林值,以追蹤 VAST 圖示備用圖片是否顯示。接著,請監聽 ICON_TAPPED
和 ICON_FALLBACK_IMAGE_CLOSED
,以便處理 VAST 圖示備用圖片周圍的廣告播放作業。請參閱下列程式碼片段,瞭解如何在進階範例中處理這項問題。
VideoPlayerController.java
// Copyright 2014 Google Inc. All Rights Reserved.
package com.google.ads.interactivemedia.v3.samples.videoplayerapp;
import android.app.UiModeManager;
import android.content.Context;
...
// Tracks if the SDK is playing an ad, since the SDK might not necessarily use
// the video player provided to play the video ad.
private boolean isAdPlaying;
// Tracks whether the SDK has a VAST icon fallback image showing.
private boolean isConnectedTvFallbackImageShowing = false;
// View that handles taps to toggle ad pause/resume during video playback.
private View playPauseToggle;
// View that we can write log messages to, to display in the UI.
private Logger log;
...
adsManager.addAdEventListener(
new AdEvent.AdEventListener() {
/** Responds to AdEvents. */
@Override
public void onAdEvent(AdEvent adEvent) {
...
case CONTENT_RESUME_REQUESTED:
// AdEventType.CONTENT_RESUME_REQUESTED is fired when the ad is
// completed and you should start playing your content.
resumeContent();
break;
case ICON_TAPPED:
// The user has tapped a VAST icon fallback image. On Android
// mobile apps, the SDK will navigate to the landing page. On
// Connected TV devices, the SDK will present a modal dialog
// containing the VAST icon fallback image.
// Check if the app is running on a TV device.
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
isConnectedTvFallbackImageShowing = true;
}
// Focus the IMA WebView for easier access to ad UI elements.
adsManager.focus();
break;
case PAUSED:
if (isConnectedTvFallbackImageShowing) {
// Do not show the controls; continue to leave the controls in
// the hands of the ads SDK.
break;
}
isAdPlaying = false;
videoPlayerWithAdPlayback.enableControls();
break;
case ICON_FALLBACK_IMAGE_CLOSED:
// The user has closed the VAST icon fallback image. This may
// be a good time to resume ad playback if the user is ready to
// continue playing the ad. This event only fires for Connected
// TV devices.
isConnectedTvFallbackImageShowing = false;
adsManager.resume();
break;
case RESUMED:
isAdPlaying = true;
videoPlayerWithAdPlayback.disableControls();
break;
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eFor optimal app functionality on Android TV using the IMA SDK, ensure your Activity is set up for TV and handles TV navigation.\u003c/p\u003e\n"],["\u003cp\u003eThe IMA SDK automatically optimizes skippable ad formats for TV and sets focus on the skip button; no extra configuration is needed.\u003c/p\u003e\n"],["\u003cp\u003eWhen using VAST icon fallback images, implement event listeners for \u003ccode\u003eICON_TAPPED\u003c/code\u003e and \u003ccode\u003eICON_FALLBACK_IMAGE_CLOSED\u003c/code\u003e to manage ad playback and user interaction, especially on Android TV where modal dialogs are used.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the provided code example in the Advanced Example to learn how to track VAST icon fallback image visibility and control ad playback accordingly in your app.\u003c/p\u003e\n"]]],[],null,["# Optimize for Android TV\n\nWe recommend several best practices to ensure your app functions properly on\nAndroid TV when using the IMA SDK for Android.\n\nGet started by familiarizing yourself with\n[developing TV apps for Android](//developer.android.com/training/tv/start/).\nSpecifically, make sure your Activity is set up for TV as explained in the\n[start guide](//developer.android.com/training/tv/start/start). You\nalso want to handle\n[TV navigation](//developer.android.com/training/tv/start/navigation) to\nmake sure users can navigate your app well on Android TV.\n\nHandle skippable ads\n--------------------\n\nThe SDK optimizes skippable formats for TV-like devices, for example by\nremoving the ability to engage with a **Learn More** button. By default, the SDK\nsets focus on the skip button when skipping is available so the ad can be\nskipped on Android TV. Therefore, no extra work is necessary to support\nskippable ads.\n\nYou can configure this by calling\n[`AdsRenderingSettings.setFocusSkipButtonWhenAvailable()`](/interactive-media-ads/docs/sdks/android/client-side/api/reference/com/google/ads/interactivemedia/v3/api/AdsRenderingSettings#setFocusSkipButtonWhenAvailable(boolean)).\n\nFor more information on what ads are supported, check out the\n[compatibility matrix](/interactive-media-ads/docs/sdks/android/compatibility).\n\nHandle VAST icon fallback images\n--------------------------------\n\nThe IMA SDK detects, renders and handles user's interaction with the VAST icon\nfallback images. Your app should listen to the `ICON_TAPPED` and\n`ICON_FALLBACK_IMAGE_CLOSED` events to handle ad playback for ads that use\n'Why this ad' (WTA).\n\nAdd a boolean to track whether a VAST icon fallback image is showing. Then,\nlisten to the `ICON_TAPPED` and `ICON_FALLBACK_IMAGE_CLOSED` to handle ad\nplayback around the VAST icon fallback image. See the following code snippet for\nan example of how this is handled in the\n[Advanced Example](//github.com/googleads/googleads-ima-android/tree/main/AdvancedExample).\n\n**VideoPlayerController.java** \n\n // Copyright 2014 Google Inc. All Rights Reserved.\n\n package com.google.ads.interactivemedia.v3.samples.videoplayerapp;\n\n import android.app.UiModeManager;\n import android.content.Context;\n\n ...\n\n // Tracks if the SDK is playing an ad, since the SDK might not necessarily use\n // the video player provided to play the video ad.\n private boolean isAdPlaying;\n\n // Tracks whether the SDK has a VAST icon fallback image showing.\n private boolean isConnectedTvFallbackImageShowing = false;\n\n // View that handles taps to toggle ad pause/resume during video playback.\n private View playPauseToggle;\n\n // View that we can write log messages to, to display in the UI.\n private Logger log;\n\n ...\n\n adsManager.addAdEventListener(\n new AdEvent.AdEventListener() {\n /** Responds to AdEvents. */\n @Override\n public void onAdEvent(AdEvent adEvent) {\n\n ...\n\n case CONTENT_RESUME_REQUESTED:\n // AdEventType.CONTENT_RESUME_REQUESTED is fired when the ad is\n // completed and you should start playing your content.\n resumeContent();\n break;\n case ICON_TAPPED:\n // The user has tapped a VAST icon fallback image. On Android\n // mobile apps, the SDK will navigate to the landing page. On\n // Connected TV devices, the SDK will present a modal dialog\n // containing the VAST icon fallback image.\n // Check if the app is running on a TV device.\n UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);\n if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {\n isConnectedTvFallbackImageShowing = true;\n }\n // Focus the IMA WebView for easier access to ad UI elements.\n adsManager.focus();\n break;\n case PAUSED:\n if (isConnectedTvFallbackImageShowing) {\n // Do not show the controls; continue to leave the controls in\n // the hands of the ads SDK.\n break;\n }\n isAdPlaying = false;\n videoPlayerWithAdPlayback.enableControls();\n break;\n case ICON_FALLBACK_IMAGE_CLOSED:\n // The user has closed the VAST icon fallback image. This may\n // be a good time to resume ad playback if the user is ready to\n // continue playing the ad. This event only fires for Connected\n // TV devices.\n isConnectedTvFallbackImageShowing = false;\n adsManager.resume();\n break;\n case RESUMED:\n isAdPlaying = true;\n videoPlayerWithAdPlayback.disableControls();\n break;"]]