针对 Android TV 进行优化
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
我们建议您遵循以下最佳实践,以确保在使用 IMA SDK for Android 时,您的应用能够在 Android TV 上正常运行。
首先,请熟悉面向 Android 开发 TV 应用。具体而言,请确保您的 activity 已按照入门指南中的说明设置为适用于 TV。您还需要处理 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;
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):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;"]]