1page.title=Handling Features Not Supported on TV 2parent.title=Designing for TV 3parent.link=index.html 4 5trainingnavtop=true 6previous.title=Optimizing Navigation for TV 7previous.link=optimizing-navigation-tv.html 8 9@jd:body 10 11<div id="tb-wrapper"> 12<div id="tb"> 13 14<h2>This lesson teaches you to</h2> 15<ol> 16 <li><a href="#WorkaroundUnsupportedFeatures">Work Around Features Not Supported on TV</a></li> 17 <li><a href="#CheckAvailableFeatures">Check for Available Features at Runtime</a></li> 18</ol> 19 20</div> 21</div> 22 23<p> 24TVs are much different from other Android-powered devices: 25</p> 26<ul> 27 <li>They're not mobile.</li> 28 <li>Out of habit, people use them for watching media with little or no interaction.</li> 29 <li>People interact with them from a distance.</li> 30</ul> 31 32<p> 33Because TVs have a different purpose from other devices, they usually don't have hardware features 34that other Android-powered devices often have. For this reason, the Android system does not 35support the following features for a TV device: 36<table> 37<tr> 38<th>Hardware</th> 39<th>Android feature descriptor</th> 40</tr> 41<tr> 42<td>Camera</td> 43<td>android.hardware.camera</td> 44</tr> 45<tr> 46<td>GPS</td> 47<td>android.hardware.location.gps</td> 48</tr> 49<tr> 50<td>Microphone</td> 51<td>android.hardware.microphone</td> 52</tr> 53<tr> 54<td>Near Field Communications (NFC)</td> 55<td>android.hardware.nfc</td> 56</tr> 57<tr> 58<td>Telephony</td> 59<td>android.hardware.telephony</td> 60</tr> 61<tr> 62<td>Touchscreen</td> 63<td>android.hardware.touchscreen</td> 64</tr> 65</table> 66</p> 67 68<p> 69This lesson shows you how to work around features that are not available on TV by: 70<ul> 71 <li>Providing work arounds for some non-supported features.</li> 72 <li>Checking for available features at runtime and conditionally activating/deactivating certain code 73 paths based on availability of those features.</li> 74</ul> 75</p> 76 77 78<h2 id="WorkaroundUnsupportedFeatures">Work Around Features Not Supported on TV</h2> 79 80<p> 81Android doesn't support touchscreen interaction for TV devices, most TVs don't have touch screens, 82and interacting with a TV using a touchscreen is not consistent with the 10 foot environment. For 83these reasons, users interact with Android-powered TVs using a remote. In consideration of this, 84ensure that every control in your app can be accessed with the D-pad. Refer back to the previous two lessons 85<a href="{@docRoot}training/tv/optimizing-layouts-tv.html">Optimizing Layouts for TV</a> and 86<a href="{@docRoot}training/tv/optimizing-navigation-tv.html">Optimize Navigation for TV</a> for 87more details 88on this topic. The Android system assumes that a device has a touchscreen, so if you want your application 89to run on a TV, you must <strong>explicitly</strong> disable the touchscreen requirement in your manifest file: 90<pre> 91<uses-feature android:name="android.hardware.touchscreen" android:required="false"/> 92</pre> 93</p> 94 95<p> 96Although a TV doesn't have a camera, you can still provide a photography-related application on a TV. 97For example, if you have an app that takes, views and edits photos, you can disable its picture-taking 98functionality for TVs and still allow users to view and even edit photos. The next section talks about how to 99deactivate or activate specific functions in the application based on runtime device type detection. 100</p> 101 102<p> 103Because TVs are stationary, indoor devices, they don't have built-in GPS. If your application uses location 104information, allow users to search for a location or use a "static" location provider to get 105a location from the zip code configured during the TV setup. 106<pre> 107LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 108Location location = locationManager.getLastKnownLocation("static"); 109Geocoder geocoder = new Geocoder(this); 110Address address = null; 111 112try { 113 address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1).get(0); 114 Log.d("Zip code", address.getPostalCode()); 115 116} catch (IOException e) { 117 Log.e(TAG, "Geocoder error", e); 118} 119</pre> 120</p> 121 122<p> 123TVs usually don't support microphones, but if you have an application that uses voice control, 124you can create a mobile device app that takes voice input and then acts as a remote control for a TV. 125</p> 126 127<h2 id="CheckAvailableFeatures">Check for Available Features at Runtime</h2> 128 129<p> 130To check if a feature is available at runtime, call 131{@link android.content.pm.PackageManager#hasSystemFeature(String)}. 132 This method takes a single argument : a string corresponding to the 133feature you want to check. For example, to check for touchscreen, use 134{@link android.content.pm.PackageManager#hasSystemFeature(String)} with the argument 135{@link android.content.pm.PackageManager#FEATURE_TOUCHSCREEN}. 136</p> 137 138<p> 139The following code snippet demonstrates how to detect device type at runtime based on supported features: 140 141<pre> 142// Check if android.hardware.telephony feature is available. 143if (getPackageManager().hasSystemFeature("android.hardware.telephony")) { 144 Log.d("Mobile Test", "Running on phone"); 145// Check if android.hardware.touchscreen feature is available. 146} else if (getPackageManager().hasSystemFeature("android.hardware.touchscreen")) { 147 Log.d("Tablet Test", "Running on devices that don't support telphony but have a touchscreen."); 148} else { 149 Log.d("TV Test", "Running on a TV!"); 150} 151</pre> 152</p> 153 154<p> 155This is just one example of using runtime checks to deactivate app functionality that depends on features 156that aren't available on TVs. 157</p>