(三)高级篇____9、设计TV上应用——处理TV上不支持的特性

负责人:cdkd123

分任务原文链接:http://docs.eoeandroid.com/training/tv/unsupported-features-tv.html

 

处理安卓电视不支持的一些功能

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

  • 电视不是手机.
  • 和手机的使用习惯有所区别,用户看电视,很少(像手机那样)和电视交互(例如触摸).
  • 即使交互也会远距离交互(例如遥控器).

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

硬件安卓特性描述符

相机

android.hardware.camera

导航GPS

android.hardware.location.gps

麦克风

android.hardware.microphone

近场通信(NFC)

android.hardware.nfc

通话

android.hardware.telephony

触屏

android.hardware.touchscreen

通过讲解如下两点,你会学到怎么在没有某些功能的情况下对安卓电视编程:

  • 针对安卓电视不支持的功能的解决办法
  • 运行时检测功能的可用性,从而让系统只执行那些和设备支持的功能相对应的代码。

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

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

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

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

因为电视固定的、室内设备,所以没有内置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);
}

安卓电视一般不支持麦克风,如果你的app用到了语音控制功能,你可以创建一个手机端的应用,用来对接电视,从而实施语音控制。

运行时检测功能的可用性

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

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

// 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!");
}

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

posted @ 2014-07-31 11:17  ╰→劉じ尛鶴  阅读(166)  评论(0)    收藏  举报