最近要开发一个tv项目,就去官网看了一些资料。自己边看边整理一些个人认为对我目前有帮助的,这里主要是对官网上的一部分信息进行了粘贴复制。有兴趣的小伙伴可以去官网查看 https://developer.android.google.cn/training/tv/,在这里我就不给大家翻译了。

Ⅰ.Get Start with TV apps

In order to make your app successful on TV devices, you must design new layouts that can be easily understood from 10 feet away, and provide navigation that works with just a directional pad and a select button.

Determine media format support

  • Supported media formats
  • DRM
  • android.drm
  • ExoPlayer
  • android.media.MediaPlayer

Prerequisites

  • Update your SDK tools to version 24.0.0 or higher
    The updated SDK tools enable you to build and test apps for TV.
  • Update your SDK with Android 5.0 (API 21) or higher
    The updated platform version provides new APIs for TV apps.
  • Create or update your app project
    In order to access new APIs for TV devices, you must create a project or modify an existing project that targets Android 5.0 (API level 21) or higher.

Declare a TV activity

An application intended to run on TV devices must declare a launcher activity for TV in its manifest. It uses a CATEGORY_LEANBACK_LAUNCHER intent filter to do this. This filter identifies your app as being enabled for TV, and lets Google Play identify it as a TV app. When a user selects your app on their TV home screen, this intent identifies which activity to launch.

  ...                                                

The second activity manifest entry in this example specifies that activity as the one to launch on a TV device.

Caution: If you do not include the CATEGORY_LEANBACK_LAUNCHER intent filter in your app, it is not visible to users running Google Play on TV devices. Also, if your app does not have this filter when you use developer tools to load it onto a TV device, the app does not appear in the TV user interface.

If you are modifying an existing app for use on TV, your app should not use the same activity layout for TV that it does for phones and tablets. Your TV app's user interface (or TV portion of your existing app) should provide a simpler interface for easy navigation using a remote control from a couch.

Declare Leanback support

Declare that your app uses the Leanback user interface required by Android TV. If you are developing an app that runs on mobile (phones, wearables, tablets, etc.) as well as Android TV, set the required attribute value to false. If you set the required attribute value to true, your app will run only on devices that use the Leanback UI.

        ...

Declare touchscreen not required

Applications that are intended to run on TV devices do not rely on touch screens for input. To make this clear, your TV app's manifest must declare that the android.hardware.touchscreen feature is not required. This setting identifies your app as being able to work on a TV device, and is required for your app to be considered a TV app in Google Play. The following code example shows how to include this manifest declaration:

        ...

Provide a home screen banner

If an application includes a Leanback launcher intent filter, it must provide a home screen banner image for each localization. The banner is the app launch point that appears on the home screen in the apps and games rows. To add the banner to your app, describe the banner in the manifest as follows:

    ...

Use the android:banner attribute with the tag to supply a default banner for all application activities, or with the tag to supply a banner for a specific activity.

The banner should be an xhdpi resource with a size of 320 x 180 px. Text must be included in the image. If your app is available in more than one language, you must provide separate versions of the banner with text for each supported language.

Change the launcher color

When a TV app launches, the system displays an animation that resembles an expanding, filled circle. To customize the color of this animation, set the android:colorPrimary attribute of your TV app or activity to a specific color. You should also set two additional transition overlap attributes to true, as shown in the following snippet from a theme resource XML file:

    

Add TV support libraries

  • v17 leanback library - Provides user interface widgets for TV apps, particularly for apps that do media playback.
  • v7 recyclerview library - Provides classes for managing the display of long lists in a memory-efficient manner. Several classes in the v17 leanback library depend on the classes in this library.
  • v7 cardview library - Provides user interface widgets for displaying information cards such as media item pictures and descriptions.
    Note: You are not required to use these support libraries for your TV app. However, we strongly recommend using them, particularly for apps that provide a media catalog browsing interface.
    If you decide to use the v17 leanback library for your app, you should note that it is dependent on the v4 support library. Thus, apps that use the leanback support library must include the following support libraries:
  • v4 support library
  • v7 recyclerview support library
  • v17 leanback support library
  • Build TV playback apps - TVs are built to entertain, so Android provides a set of user interface tools and widgets for building TV apps that play videos and music, and let users browse for the content they want.
  • Help users find your content on TV - With all the content choices at users' fingertips, helping them find content they enjoy is almost as important as providing that content. This training discusses how to surface your content on TV devices.

Run TV apps

Run on a real device

1.Use a USB cable to connect your TV device to your development machine. If needed, refer to documentation provided by your device manufacturer.
2.On your TV device, navigate to Settings.
3.In the Device row, select About.
4.Scroll down to Build and select Build several times until you get the message, "You are now a developer!"
5.Return to Settings. In the Preferences row, select Developer options.
6.Select Debugging > USB debugging and select On.
7.Navigate back to the TV home screen.

Run on a virtual device
  1. Start the AVD Manager. For more information, see the AVD Manager help.
  2. In the AVD Manager dialog, click the Device Definitions tab.
  3. Select one of the Android TV device definitions and click Create AVD.
  4. Select the emulator options and click OK to create the AVD.

Ⅱ.Handle TV hardware

TV hardware is substantially different from other Android devices. TVs do not include some of the hardware features found on other Android devices, such as touch screens, cameras, and GPS receivers. TVs are also completely dependent on secondary hardware devices. In order for users to interact with TV apps, they must use a remote control or game pad. When you build an app for TV, you must carefully consider the hardware limitations and requirements of operating on TV hardware.

Declaring hardware requirements for TV

Android apps can declare hardware feature requirements in the app manifest to ensure that they do not get installed on devices that do not provide those features. If you are extending an existing app for use on TV, closely review your app's manifest for any hardware requirement declarations that might prevent it from being installed on a TV device

**Note:** Some features have subfeatures like *android.hardware.camera.front*, as described in the [Feature reference](https://developer.android.google.cn/guide/topics/manifest/uses-feature-element.html#features-reference). Be sure to mark as *required="false"* any subfeatures also used in your app.
Caution: Declaring a hardware feature as required by setting its value to true prevents your app from being installed on TV devices or appearing in the Android TV home screen launcher.

Pausing playback during low-power mode

Some TV devices support a low-power mode when the user switches the device off. Instead of shutting down, the device disables the display and keeps Android TV running in the background. Audio output is still enabled in this mode, so your app should stop any currently playing content when the device is in low-power mode.

To avoid playback during low-power mode, override [onStop()](https://developer.android.google.cn/reference/android/app/Activity.html#onStop()) and stop any currently playing content:

@Overridepublic void onStop() {  // App-specific method to stop playback  stopPlayback();  super.onStop();}

Ⅲ.Handle controller disconnects

Controllers for TV are frequently Bluetooth devices which may attempt to save power by periodically going into sleep mode and disconnecting from the TV device. This means that an app might be interrupted or restarted if it is not configured to handle these reconnect events. These events can happen in any of the following circumstances:

While watching a video which is several minutes long, a D-Pad or game controller goes into sleep mode, disconnects from the TV device and then reconnects later on.
During gameplay, a new player joins the game using a game controller that is not currently connected.
During gameplay, a player leaves the game and disconnects a game controller.
Any TV app activity that is subject to disconnect and reconnect events must be configured to handle reconnection events in the app manifest. The following code sample demonstrates how to enable an activity to handle configuration changes, including a keyboard or navigation device connecting, disconnecting, or reconnecting:

              ...

This configuration change allows the app to continue running through a reconnection event, rather than being restarted by the Android framework, which is not a good user experience.

Handle button events

When the user clicks a button on a controller, your app receives an event with a KeyEvent. The intended behavior for the button might be a media event (like play, pause, or stop) or it could be a TV-type event (like selection or navigation). In order to provide a good user experience, your app should assign consistent behavior to controller buttons.

TV UI events

Buttons that generate these KeyEvents should be handled by the app according to table below.

KeyEvent Behavior
BUTTON_B, BACK Back
BUTTON_SELECT, BUTTON_A, ENTER, DPAD_CENTER, KEYCODE_NUMPAD_ENTER Selection
DPAD_UP, DPAD_DOWN, DPAD_LEFT, DPAD_RIGHT Navigation
Media events

When the user is watching media, buttons that generate these KeyEvents should be handled by app according to table below. If your app is controlling a MediaSession, it should call one of the TransportControls methods as shown below. Note that the selection buttons act as Play/Pause buttons in this context.

KeyEvent TransportControls call Behavior
BUTTON_SELECT, BUTTON_A, ENTER, DPAD_CENTER, KEYCODE_NUMPAD_ENTER pause() Play
BUTTON_START, BUTTON_SELECT, BUTTON_A, ENTER, DPAD_CENTER, KEYCODE_NUMPAD_ENTER pause() Pause
BUTTON_R1 skipToNext() Skip to next
BUTTON_L1 skipToPrevious() Skip to previous
DPAD_RIGHT, BUTTON_R2, AXIS_RTRIGGER, AXIS_THROTTLE fastForward() Fast forward
DPAD_LEFT, BUTTON_L2, AXIS_LTRIGGER, AXIS_BRAKE rewind() Rewind
No KeyEvent is associated with Stop) stop() Stop

Ⅳ.Build layouts for TV

A TV screen is typically viewed from about 10 feet away, and while it is much larger than most other Android device displays, this type of screen does not provide the same level of precise detail and color as a smaller device. These factors require you to create app layouts with TV devices in mind in order to create a useful and enjoyable user experience.

Use layout themes for TV

Leanback theme

The v17 leanback support library includes Theme.Leanback, a theme for TV activities that provides a consistent visual style. We strongly recommend using this theme for any TV app built with the v17 leanback classes. The following code sample shows how to apply this theme to an activity:

**Caution:**The leanback theme does not include an action bar, since there none in an Android TV app. If your app uses support fragments, like BrowseSupportFragment, your activity must extend FragmentActivity. Do not use AppCompatActivitywhich tries to theme the action bar and produces the error

java.lang.RuntimeException: Unable to start activity ComponentInfo{...} :java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with thisactivity.
NoTitleBar theme

The title bar is a standard user interface element for Android apps on phones and tablets, but it is not appropriate for TV apps. If you are not using v17 leanback classes, you should apply this theme to your TV activities to suppress the display of a title bar. The following code example from a TV app manifest demonstrates how to apply this theme to remove the display of a title bar:

  ...      ...  

Build basic TV layouts

Layouts for TV devices should follow some basic guidelines to ensure they are usable and effective on large screens. Follow these tips to build landscape layouts optimized for TV screens:

  • Build layouts with a landscape orientation. TV screens always display in landscape mode.
  • Put on-screen navigation controls on the left or right side of the screen and save the vertical space for content.
  • Create UIs that are divided into sections, using Fragments, and use view groups like GridView instead of ListView to make better use of the horizontal screen space.
  • Use view groups such as RelativeLayout or LinearLayout to arrange views. This approach allows the system to adjust the position of the views to the size, alignment, aspect ratio, and pixel density of a TV screen.
  • Add sufficient margins between layout controls to avoid a cluttered UI.
Overscan

Screen elements that must be visible to the user at all times should be positioned within the overscan safe area. Adding a 5% margin of 48dp on the left and right edges and 27dp on the top and bottom edges to a layout ensures that screen elements in that layout will be within the overscan safe area.Example:

<?xml version="1.0" encoding="utf-8"?>                  

**Caution:** Do not apply overscan margins to your layout if you are using the v17 leanback classes, such asBrowseFragmentor related widgets, as those layouts already incorporate overscan-safe margins.

Build useable text and controls

The text and controls in a TV app layout should be easily visible and navigable from a distance. Follow these tips to make your user interface elements easier to see from a distance:

  • Break text into small chunks that users can quickly scan.
  • Use light text on a dark background. This style is easier to read on a TV.
  • Avoid lightweight fonts or fonts that have both very narrow and very broad strokes. Use simple sans-serif fonts and anti-aliasing to increase readability.
  • Use Android's standard font sizes:
  • Ensure that all your view widgets are large enough to be clearly visible to someone sitting 10 feet away from the screen (this distance is greater for very large screens). The best way to do this is to use layout-relative sizing rather than absolute sizing, and density-independent pixel (dip) units instead of absolute pixel units. For example, to set the width of a widget, use wrap_content instead of a pixel measurement, and to set the margin for a widget, use dip values instead of px values.

For more information about density-independent pixels and building layouts to handle larger screen sizes, see Supporting multiple screens.

Manage layout resources for TV

The common high-definition TV display resolutions are 720p, 1080i, and 1080p. Your TV layout should target a screen size of 1920 x 1080 pixels, and then allow the Android system to downscale your layout elements to 720p if necessary. In general, downscaling (removing pixels) does not degrade your layout presentation quality. However, upscaling can cause display artifacts that degrade the quality of your layout and have a negative impact on the user experience of your app.
To get the best scaling results for images, provide them as 9-patch image elements if possible. If you provide low quality or small images in your layouts, they will appear pixelated, fuzzy, or grainy, which is not a good experience for the user. Use high-quality images instead.

Handle large bitmaps

TV devices, like any other Android device, have a limited amount of memory. If you build your app layout with very high-resolution images or use many high-resolution images in the operation of your app, it can quickly run into memory limits and cause out of memory errors. To avoid these types of problems, follow these tips:

  • Load images only when they are displayed on the screen. For example, when displaying multiple images in a GridView or Gallery, only load an image when getView() is called on the view's Adapter.
  • Call recycle() on Bitmap views that are no longer needed.
  • Use WeakReference for storing references to Bitmap objects in an in-memory `Collection.
  • If you fetch images from the network, use AsyncTask` to fetch and store them on the device for faster access. Never do network transactions on the application's main user interface thread.
  • Scale down large images to a more appropriate size as you download them; otherwise, downloading the image itself may cause an out of memory exception.
    For more information on getting the best performance when working with images, see Display bitmaps efficiently.

Avoid layout anti-patterns

There are a few approaches to building layouts that you should avoid because they do not work well on TV devices and lead to bad user experiences. Here are some user interface approaches you should specifically not use when developing a layout for TV.

  • Re-using phone or tablet layouts - Do not reuse layouts from a phone or tablet app without modification. Layouts built for other Android device form factors are not well suited for TV devices and should be simplified for operation on a TV.
  • ActionBar - While this user interface convention is recommended for use on phones and tablets, it is not appropriate for a TV interface. In particular, using an action bar options menu (or any pull-down menu for that matter) is strongly discouraged, due to the difficulty in navigating such a menu with a remote control.
  • ViewPager - Sliding between screens can work great on a phone or tablet, but don't try this on a TV!

For more information on designing layouts that are appropriate to TV, see the TV design guide.

Provide effective advertising

For the living room environment, we recommend you use video ads solutions that are full-screen and dismissable within 30 seconds. Functionality for advertising on Android TV, such as dismiss buttons and clickthroughs, must be accessible using the D-pad rather than touch.

Android TV does not provide a web browser. Your ads must not attempt to launch a web browser or redirect to Google Play Store content that is not approved for Android TV devices.
**Note:** You can use theWebViewclass for logins to services like Google+ and Facebook.

Ⅴ.Create TV navigation

Note: You should only use these attributes to modify the navigation order if the default order that the system applies does not work well.
he following code sample shows how to define the next control to receive focus for a TextView layout object:

The following table lists all of the available navigation attributes for Android user interface widgets:

Attribute Function
nextFocusDown Defines the next view to receive focus when the user navigates down.
nextFocusLeft Defines the next view to receive focus when the user navigates left.
nextFocusRight Defines the next view to receive focus when the user navigates right.
nextFocusUp Defines the next view to receive focus when the user navigates up.

To use one of these explicit navigation attributes, set the value to the ID (android:id value) of another widget in the layout. You should set up the navigation order as a loop, so that the last control directs focus back to the first one.

Provide clear focus and selection

The success of an app's navigation scheme on TV devices is depends on how easy it is for a user to determine what user interface element is in focus on screen.
Your app layout and implementation should use color, size, animation, or a combination of these attributes to help users easily determine what actions they can take next. Use a uniform scheme for indicating focus across your application.

Ⅵ.Request background playback

Normally, when the user clicks on Home to display the TV launcher, the activity pauses. However, your app can request background playback, in which the activity continues playing behind the TV launcher.

To request background playback, call requestVisibleBehind(). Be sure to clean up media resources if the activity stops being visible. For example, you should free media resources if requestVisibleBehind() returns false to indicate that the request failed, or if the system calls your override of onVisibleBehindCanceled().

@Overridepublic void onPause() {  super.onPause();  if (mVideoView.isPlaying()) {    // Argument equals true to notify the system that the activity    // wishes to be visible behind other translucent activities    if (! requestVisibleBehind(true)) {      // App-specific method to stop playback and release resources      // because call to requestVisibleBehind(true) failed      stopPlayback();    }  } else {    // Argument equals false because the activity is not playing    requestVisibleBehind(false);  }}@Overridepublic void onVisibleBehindCanceled() {  // App-specific method to stop playback and release resources  stopPlayback();  super.onVisibleBehindCanceled();}

**The methodrequestVisibleBehind()was deprecated in API level 26.** It will be removed in a future release. The functionality described on this page is not supported in Android version 8.0 and later.

错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下

更多相关文章

  1. 你的Android库是否还在Application中初始化?
  2. 【Android(安卓)Studio】将一个Module直接复制到另一个Project中
  3. JS复制各种链接
  4. Android模拟器调试“掉线”问题解决方法
  5. android webview中使用Java调用JavaScript方法并获取返回值
  6. Android中Activity常用功能设置小结(包括全屏、横竖屏等)
  7. Android自定义属性,attr format取值类型
  8. Android通过LIstView显示文件列表的两种方法介绍
  9. android仿IOS7的两种对话框

随机推荐

  1. Android——读取含有双字节字符的TXT文件
  2. android sdk 下载地址
  3. android锁屏唤醒并解锁屏幕
  4. android 时间获取以及时间格式化
  5. android几种开源项目源码地址
  6. android 回调函数二:应用实例
  7. Android NDK编译libPng为静态库
  8. Real Android apps leveraging db4o pers
  9. Android Fire Project(1)Recall Env and
  10. android录制更大视频文件的修改