[中英文對照]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

1.jpg

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.

這段代碼演示的就是在安卓電視上怎麼根據硬件不支持的功能禁用軟件對應的功能。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章