助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
> 编写:[awong1900](https://github.com/awong1900) - 原文:[http://developer.android.com/training/tv/start/start.html](http://developer.android.com/training/tv/start/start.html) TV应用使用与手机和平板同样的架构。这种相似性意味着你可以修改现有的应用到TV设备或者用以前安卓应用的经验开发TV应用。 > **Important**: 在Google Play中的TV应用应满足一些特定要求。更多信息, 参考[TV App Quality](http://developer.android.com/distribute/essentials/quality/tv.html)中的要求列表。 本课程介绍如何准备您的TV应用开发环境,和使应用能够运行在TV设备上所必须的最小改变。 ### 创建TV项目 本节讨论如何修改已有的应用或者新建一个应用使之能够运行在电视设备上。在TV设备运行的应用必须使用这些主要组件: - **[Activity](# "An activity represents a single screen with a user interface.") for TV** (必须) - 在您的application manifest中, 声明一个可在TV设备上运行的[activity](# "An activity represents a single screen with a user interface.")。 - **TV Support Libraries** (可选) - 这些支持库[Support Libraries](http://developer.android.com/training/tv/start/start.html#tv-libraries) 可以提供搭建TV用户界面的控件。 ### 前提条件 在创建TV应用前, 必须做以下事情: - [更新SDK tools到版本24.0.0或更高](http://developer.android.com/sdk/installing/adding-packages.html#GetTools) 最新的SDK tools能确保编译和测试TV应用 - [更新SDK为Android 5.0 (API 21)或更高](http://developer.android.com/sdk/installing/adding-packages.html#GetTools) 最新的platform版本提供TV应用的新API - [创建或更新应用项目](http://developer.android.com/sdk/installing/create-project.html) 为了支持TV新API, 你必须设置新项目或者修改原项目的目标平台为Android 5.0 (API level 21)或者更高。 ### 声明一个TV [Activity](# "An activity represents a single screen with a user interface.") 一个应用想要运行在TV设备中,必须在它的manifest中定义一个启动[activity](# "An activity represents a single screen with a user interface."),用intent filter包含[CATEGORY_LEANBACK_LAUNCHER](http://developer.android.com/reference/android/content/Intent.html#CATEGORY_LEANBACK_LAUNCHER)。这个filter表明你的应用是在TV上可用,并且为Google Play上发布TV应用所必须。定义这个intent也意味着在点击app在TV主屏幕的图标时,打开的就是这个[activity](# "An activity represents a single screen with a user interface.")。 接下来的代码片段显示如何在manifest中包含这个intent filter: ~~~ <application android:banner="@drawable/banner" > ... <activity android:name="com.example.android.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.android.TvActivity" android:label="@string/app_name" android:theme="@style/Theme.Leanback"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LEANBACK_LAUNCHER" /> </intent-filter> </activity> </application> ~~~ 例子中第二个[activity](# "An activity represents a single screen with a user interface.")的manifest是TV设备中的一个启动入口。 > **Caution**:如果在你的应用中不包含[CATEGORY_LEANBACK_LAUNCHER](http://developer.android.com/reference/android/content/Intent.html#CATEGORY_LEANBACK_LAUNCHER) intent filter,它不会出现在TV设备的Google Play商店中。并且,即使你把不包含此filter的应用用开发工具装载到TV设备中,应用仍然不会出现在TV用户界面上。 如果你正在为TV设备修改现有的应用,不应该与手机和平板用同样的[activity](# "An activity represents a single screen with a user interface.")布局。TV的用户界面(或者现有应用的TV部分)应该提供一个更简单的界面,更容易坐在沙发上用遥控器操作。TV应用设计指南,参考[TV Design](http://developer.android.com/design/tv/index.html)指导。查看TV界面布局的最低要求,参考:[Building TV Layouts](http://developer.android.com/training/tv/start/layouts.html)。 ### 声明Leanback支持 安卓TV需要你的应用使用Leanback用户界面。如果你正在开发一个运行在移动设备(手机,可穿戴,平板等等)同时也包含安卓TV的应用,设置`required`属性为`false`。因为如果设置为`true`,你的应用将仅能运行在用Leanback UI的设备上。 ~~~ <manifest> <uses-feature android:name="android.software.leanback" android:required="false" /> ... </manifest> ~~~ ### 声明不需要触屏 运行在TV设备上的应用不依靠触屏去输入。为了清楚表明这一点,TV应用的manifest必须声明`android.hardware.touchscreen`为不需要。这个设置表明应用能够工作在TV设备上,并且也是Google Play认定你的应用为TV应用的要求。接下来的示例代码展示这个manifest声明: ~~~ <manifest> <uses-feature android:name="android.hardware.touchscreen" android:required="false" /> ... </manifest> ~~~ > **Caution**:必须在manifest中声明触屏是不需要的,否则应用不会出现在TV设备的Google Play商店中。 ### 提供一个主屏幕横幅 如果应用包含一个Leanback的intent filter,它必须提供每个语言的主屏幕横幅。横幅是出现在应用和游戏栏的主屏的启动点。在manifest中这样描述横幅: ~~~ <application ... android:banner="@drawable/banner" > ... </application> ~~~ 在[`application`](http://developer.android.com/guide/topics/manifest/application.html)中添加[`android:banner`](http://developer.android.com/guide/topics/manifest/application-element.html#banner)属性为所有的应用[activity](# "An activity represents a single screen with a user interface.")提供默认的横幅,或者在特定的[activity](# "An activity represents a single screen with a user interface.")的[`activity`](http://developer.android.com/guide/topics/manifest/activity-element.html)中添加横幅。 在UI模式和TV设计指导中查看[Banners](http://developer.android.com/design/tv/patterns.html#banner)。 ### 添加TV支持库 Android SDK包含用于TV应用的支持库。这些库为TV设备提供API和用户界面控件。这些库位于`<sdk>/extras/android/support/`目录。以下是这些库的列表和它们的作用介绍: - [v17 leanback library](http://developer.android.com/tools/support-library/features.html#v17-leanback) - 提供TV应用的用户界面控件,特别是用于媒体播放应用的。 - [v7 recyclerview library](http://developer.android.com/tools/support-library/features.html#v7-recyclerview) - 提供了内存高效方式的长列表的管理显示类。有一些v17 leanback库的类依赖于本库的类。 - [v7 cardview library](http://developer.android.com/tools/support-library/features.html#v7-cardview) - 提供显示信息卡的用户界面控件,如媒体图片和描述。 > **Note**:TV应用中可以不用这些库。但是,我们强烈推荐你使用它们,特别是为应用提供媒体目录浏览界面时。 如果你决定用`v17 leanback library`,你应该注意它依赖于[v4 support library](http://developer.android.com/tools/support-library/features.html#v4)。这意味着要用leanback支持库必须包含以下所有的支持库: - v4 support library - v7 recyclerview support library - v17 leanback support library `v17 leanback library`包含资源文件,需要你在应用中采取特定的步骤去包含它。插入带资源文件的支持库的说明,查看[Support Library Setup](http://developer.android.com/tools/support-library/setup.html#libs-with-res)。 ### 创建TV应用 在完成上面的步骤之后,到了给大屏幕创建应用的时候了!检查一下这些额外的专题可以帮助您创建TV应用: - [创建TV播放应用](http://developer.android.com/training/tv/playback/index.html) - TV主要是用来娱乐,因此安卓提供了一套用户界面工具和控件,用来创建视频和音乐的TV应用,并且让用户浏览想看到的内容。 - [帮助用户找到TV内容](http://developer.android.com/training/tv/discovery/index.html) - 因为所有的内容选择操作都用手指操作遥控器,所以帮助用户找到想要的内容几乎和提供内容同样重要。这个主题讨论如何在TV设备中处理内容。 - [TV游戏](http://developer.android.com/training/tv/games/index.html) - TV设备是非常好的游戏平台。参考这个主题去创造更好的TV游戏体验。 ### 运行TV应用 在开发过程中运行应用是一个重要的部分。在安卓SDK中的AVD管理器提供了创建虚拟TV设备的功能,可以让应用在虚拟设备中运行和测试。 创建一个虚拟TV设备 1. 打开AVD管理器。更多信息,参考[AVD管理器](http://developer.android.com/tools/help/avd-manager.html)帮助。 1. 在AVD管理器窗口,点击**Device Definitions**标签。 1. 选择安卓一个TV设备定义,并且点击**Create AVD**。 1. 选择模拟器选项并且点击**OK**创建AVD。 > **Note**:获得TV模拟器设备的最佳性能,打开**Use Host GPU option**,支持虚拟设备加速。更多模拟器硬件加速信息,参考[Using the Emulator](http://developer.android.com/tools/devices/emulator.html#acceleration)。 在虚拟设备中测试应用 1. 在开发环境中编译TV应用。 1. 从开发环境中运行应用并选择目标为TV虚拟设备。 更多模拟器信息:[Using the Emulator](http://developer.android.com/tools/devices/emulator.html)。 用Android Studio部署应用到模拟器,查看[Debugging with Android Studio](http://developer.android.com/sdk/installing/studio-debug.html)。用带ADT插件的Eclipse部署应用到模拟器,查看[Building and Running from Eclipse with ADT ](http://developer.android.com/tools/building/building-eclipse.html)。 [下一节: 处理TV硬件](#)