Handling Features Not Supported on TV 在电视上处理不支持的功能

本文深入探讨了电视设备与Android系统之间的不兼容性,详细列举了电视设备无法支持的Android硬件特性,并提供了针对这些特性的工作绕过方法。包括如何通过配置manifest文件禁用触摸屏需求,如何在运行时检查可用特性并有条件地激活或停用代码路径,以及如何在没有内置GPS的情况下提供位置信息解决方案等。

TVs are much different from other Android-powered devices:http://blog.youkuaiyun.com/sergeycao

  • They're not mobile.
  • 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:

HardwareAndroid feature descriptor
Cameraandroid.hardware.camera
GPSandroid.hardware.location.gps
Microphoneandroid.hardware.microphone
Near Field Communications (NFC)android.hardware.nfc
Telephonyandroid.hardware.telephony
Touchscreenandroid.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 lessonsOptimizing Layouts for TV and Optimize 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 mustexplicitly disable the touchscreen requirement in your manifest file:

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

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.

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.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值