https://developer.android.google.cn/training/tv/start/hardware.html#handle-features
TV 不同于其它 Android 设备,它不具备一些硬件特性,比如触摸屏、相机、GPS等,并且依赖外部设备。在开发 TV 应用的时候,应当充分考虑这些因素。
接下来说说如果检测应用是否运行在 TV 上,如何处理不支持的硬件特性,讨论如何处理 TV 设备控制器的要求。
Check for a TV Device
检测下当前是否是 TV
public static final String TAG = "DeviceTypeRuntimeCheck";
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device")
} else {
Log.d(TAG, "Running on a non-TV Device")
}
比如通过 Intent 启动时,可能需要检测下当前是否是TV,以便于决定启动 TV Activity or Phone Activity。
Handle Unsupported Hardware Features
不支持的特性
Touchscreen android.hardware.touchscreen
Touchscreen emulator android.hardware.faketouch
Telephony android.hardware.telephony
Camera android.hardware.camera
Near Field Communications (NFC) android.hardware.nfc
GPS android.hardware.location.gps
Microphone [1] android.hardware.microphone
Sensors android.hardware.sensor
Screen in portrait orientation android.hardware.screen.portrait
Declaring hardware requirements for TV
声明一个特性 android:required=”true” 时,如果硬件不具备该特性,将会阻止安装。
可以声明这些特性来阻止其被安装在 TV 上。
如果有需要可以在运行的时候检测这些硬件是否存在,来决定接下来的行为。
<uses-feature android:name="android.hardware.touchscreen"
android:required="false"/>
<uses-feature android:name="android.hardware.faketouch"
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.location.gps"
android:required="false"/>
<uses-feature android:name="android.hardware.microphone"
android:required="false"/>
<uses-feature android:name="android.hardware.sensor"
android:required="false"/>
Declaring permissions that imply hardware features
声明权限会隐含硬件特性
一些 uses-permission 声明隐含了硬件特性。这些行为意味着在应用中请求一些权限能导致应用不能安装和使用在TV设备上。下面普通的权限请求包含了一个隐式的硬件特性需求:
RECORD_AUDIO android.hardware.microphone
CAMERA android.hardware.camera and android.hardware.camera.autofocus
ACCESS_COARSE_LOCATION android.hardware.location and android.hardware.location.network
ACCESS_FINE_LOCATION android.hardware.location and android.hardware.location.gps
Checking for hardware features
使用 hasSystemFeature(String) 检测硬件特性
// Check if the telephony hardware feature is available.
if (getPackageManager().hasSystemFeature("android.hardware.telephony")) {
Log.d("HardwareFeatureTest", "Device can make phone calls");
}
// Check if android.hardware.touchscreen feature is available.
if (getPackageManager().hasSystemFeature("android.hardware.touchscreen")) {
Log.d("HardwareFeatureTest", "Device has a touch screen.");
}
Handling Controllers
遥控器或游戏手柄,可能会在一段时间不使用后休眠,这需要处理一下,否则会出现重建 Activity 的情况,就是手机上的响应配置变化,包括键盘或者操作设备连接,断开连接,或者重新连接。
<activity
android:name="com.example.android.TvActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|navigation"
android:theme="@style/Theme.Leanback">
<intent-filter>
<action android:name="android.intent.action.MAIN" ></action>
<category android:name="android.intent.category.LEANBACK_LAUNCHER" ></category>
</intent-filter>
...
</activity>