[中英文对照]android Designing for TV(三) ------ Handling Features Not Supported on TV 在TV上处理不支持的功能

TVs are much different from other Android-powered devices:

电视远比其他安卓设备复杂

  • They're not mobile.
  • 它(TV)不是手机.
  • Out of habit, people use them for watching media with little or no interaction.
  • 和手机操作有不同的习惯, 人们很少和电视互动,当他们看电视的时候.
  • People interact with them from a distance.
  • 即使交互也会远距离交互(例如遥控器).

Because TVs have a different purpose from other devices, they usually don't have hardware features that other Android-powered devices often have. For this reason, the Android system does not support the following features for a TV device:

因为电视和其他安卓设备用途不同, 通常有很多硬件功能上的差异,种种原因,安卓电视就不支持如下功能

Hardware Android feature descriptor
Camera android.hardware.camera
GPS android.hardware.location.gps
Microphone android.hardware.microphone
Near Field Communications (NFC) android.hardware.nfc
Telephony android.hardware.telephony
Touchscreen android.hardware.touchscreen


This lesson shows you how to work around features that are not available on TV by:

这节课将向你们展示,你会学到怎么在没有某些功能的情况下对安卓电视编程:

  • Providing work arounds for some non-supported features.
  • 针对安卓电视不支持的功能的解决办法
  • Checking for available features at runtime and conditionally activating/deactivating certain code paths based on availability of those features
  • 运行时检测功能的可用性,从而让系统只执行那些和设备支持的功能相对应的代码。

Work Around Features Not Supported on TV

针对安卓电视不支持的功能的解决办法

Android doesn't support touchscreen interaction for TV devices, most TVs don't have touch screens, and interacting with a TV using a touchscreen is not consistent with the 10 foot environment. For these reasons, users interact with Android-powered TVs using a remote. In consideration of this, ensure that every control in your app can be accessed with the D-pad. Refer back to the previous two lessons Optimizing Layouts for TV andOptimize Navigation for TV for more details on this topic. The Android system assumes that a device has a touchscreen, so if you want your application to run on a TV, you must explicitly disable the touchscreen requirement in your manifest file:

安卓电视不提供触摸屏接口,大部分电视都没有触摸屏,像(电视机和人相距)10尺的距离,让任何电视使用触摸的方式也不现实,所以,我们一般使用遥控器和电视交互,考虑到这点,就得确保应用中的所有控制都可以用方向键完成。关于优化电视布局和导航布局的话题的相关细节可以参考前两节课。安卓系统是默认设备带有触摸屏的,所以如果系统在电视上运行,就得在配置文件mainfest配置一下,不需要触摸屏:


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

Although a TV doesn't have a camera, you can still provide a photography-related application on a TV. For example, if you have an app that takes, views and edits photos, you can disable its picture-taking functionality for TVs and still allow users to view and even edit photos. The next section talks about how to deactivate or activate specific functions in the application based on runtime device type detection.

Because TVs are stationary, indoor devices, they don't have built-in GPS. If your application uses location information, allow users to search for a location or use a "static" location provider to get a location from the zip code configured during the TV setup.

即使电视没有摄像头,我们仍然可以在安卓电视里一个图像处理程序,不过此程序只供用户查看和编辑图片,拍照功能禁用就可以了。下一段将讲解怎么启用或禁用一些基于运行时设备类型检测的特殊功能。

因为电视固定的、室内设备,所以没有内置GPS。如果你的程序需要用到地理位置信息,要么让用户(联网)查询,要么使用"static location provider" 得到这些信息,这种方式是通过查询邮编完成的。

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation("static");
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);
}

TVs usually don't support microphones, but if you have an application that uses voice control, you can create a mobile device app that takes voice input and then acts as a remote control for a TV.

安卓电视果断也不支持麦克风,但是我们可以在安卓电视上运行一个语音控制的小程序,然后再在手机上运行一个接受语音输入的小程序,这样安卓电视就可以远程控制手机了。

Check for Available Features at Runtime

运行时检测功能的可用性

To check if a feature is available at runtime, call hasSystemFeature(String). This method takes a single argument : a string corresponding to the feature you want to check. For example, to check for touchscreen, usehasSystemFeature(String) with the argument FEATURE_TOUCHSCREEN.

如果想查询哪些功能是否可用,调用方法hasSystemFeature(String)) . 该方法就一个参数,String类型,表示你想查询的功能类型,例如,传参FEATURE_TOUCHSCREEN,返回(设备)是否支持触屏功能。

The following code snippet demonstrates how to detect device type at runtime based on supported features:

以下代码片段演示了如何在程序运行时检测设备类型,这种检测基于设备支持的功能(根据支持的功能给设备分个类):

// Check if android.hardware.telephony feature is available.
if (getPackageManager().hasSystemFeature("android.hardware.telephony")) {
   Log.d("Mobile Test", "Running on phone");
// Check if android.hardware.touchscreen feature is available.
} else if (getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {
   Log.d("Tablet Test", "Running on devices that don't support telphony but have a touchscreen.");
} else {
    Log.d("TV Test", "Running on a TV!");
}

This is just one example of using runtime checks to deactivate app functionality that depends on features that aren't available on TVs.

这段代码演示的就是在安卓电视上怎么根据硬件不支持的功能禁用软件对应的功能。


你可能感兴趣的:(android,电视)