谷歌电视相关开发api

#### google tv 1 设置tv App

1.升级开发工具到最新版本

2.manifest设置

2.1 android:theme="@style/Theme.Leanback"

2.2 <category android:name="android.intent.category.LEANBACK_LAUNCHER" />

<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>

3 触屏支持 不支持触屏操作

<uses-feature android:name="android.hardware.touchscreen"

android:required="false" />

4. 提供一个横幅用于所有的activity,或者针对每一个activity设置属性

<application

...

android:banner="@drawable/banner" >

...

</application>

5.添加Android TV 支持库 目录 <sdk>/extras/android/support/

5.1 v17 leanback library 提供tv用户界面部件,特别用于媒体应用

5.2 v7 recycleview library 高效管理长列表内容内存,leanback需要

5.3 v7 cardview library 显示信息卡片 特别是媒体图片和描述

但是,以上支持库不是必须使用的,但是推荐在媒体分类的浏览的应用中使用。

如果你使用了leanback 包,就不用使用 v4包,v7reycle包河v17 leanbackback

包。

v17包包含一些资源文件,必须把那些资源文件导入到你的app。

和v7包的加入方式一样:https://developer.android.com/tools/support-library/setup.html#libs-with-res

6 硬件相关检测 api13

UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);

if (uiModeManager.getCurrentModeType()==Configuration.UI_MODE_TYPE_TELEVISION) {

// tv 模式

}else if (uiModeManager.getCurrentModeType()==Configuration.UI_MODE_TYPE_CAR) {

//汽车

}

7.在tv上,不支持的硬件特性

触屏操作 , 拨打电话,视频,近场通信,gps定位,麦克风支持

HardwareAndroid feature descriptor

--------------------------------------------------------------------------------

Touchscreenandroid.hardware.touchscreen

Telephonyandroid.hardware.telephony

Cameraandroid.hardware.camera

Near Field Communications (NFC)android.hardware.nfc

GPSandroid.hardware.location.gps

Microphoneandroid.hardware.microphone

设置:

<uses-feature android:name="android.hardware.touchscreen"

android:required="false"/>

<uses-feature android:name="android.hardware.telephony"

android:required="false"/>

<uses-feature android:name="android.hardware.camera"

android:required="false"/>

<uses-feature android:name="android.hardware.nfc"

android:required="false"/>

<uses-feature android:name="android.hardware.gps"

android:required="false"/>

<uses-feature android:name="android.hardware.microphone"

android:required="false"/>

如果,设置为true,你的程序可能就不会出现在电视设备选项,和出现在tv设备的主屏幕上。

8. 权限permissions 和硬件特性

Permission 权限Implied hardware feature需要硬件特性

--------------------------------------------------------------

RECORD_AUDIOandroid.hardware.microphone

CAMERAandroid.hardware.camera and android.hardware.camera.autofocus

ACCESS_COARSE_LOCATIONandroid.hardware.location and android.hardware.location.network

ACCESS_FINE_LOCATIONandroid.hardware.location and android.hardware.location.gps

8.1 检查系统是否支持这些硬件特性

// 检查是否支持电话特性

if (getPackageManager().hasSystemFeature("android.hardware.telephony")) {

Log.d("HardwareFeatureTest", "Device can make phone calls");

}

// 检查是否支持屏幕滑动

if (getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {

Log.d("HardwareFeatureTest", "Device has a touch screen.");

}

8.2 Touch screen android不支持的触摸屏,因为电视观看环境不统一。

8.3 Camera ,虽然没有摄像头功能,但是还是支持图片的查看和编辑。

<uses-feature android:name="android.hardware.camera" android:required="false" />

// Check if the camera hardware feature is available.

if (getPackageManager().hasSystemFeature("android.hardware.camera")) {

Log.d("Camera test", "Camera available!");

} else {

Log.d("Camera test", "No camera available. View and edit features only.");

}

8.4 GPS功能

android不支持GPS功能,但是你还是可以要求用户搜索一个地址配置到电视设备上。,

// Request a static location from the location manager

LocationManager locationManager = (LocationManager) this.getSystemService(

Context.LOCATION_SERVICE);

Location location = locationManager.getLastKnownLocation("static");

// Attempt to get postal or zip code from the static location object

Geocoder geocoder = new Geocoder(this);

Address address = null;

try {

address = geocoder.getFromLocation(location.getLatitude(),

location.getLongitude(), 1).get(0);

Log.d("Zip code", address.getPostalCode());

} catch (IOException e) {

Log.e(TAG, "Geocoder error", e);

}

9 手持设备电视控制器 Handling Controllers