Android TV-Handling TV Hardware

desaco發表於2016-01-16

 》After you have completed the steps described above, it's time to start building apps for the big screen! Check out these additional topics to help you build your app for TV:

  • Building TV Playback Apps - TVs are built to entertain, so Android provides a set of user interface tools and widgets for building TV apps that play videos and music, and let users browse for the content they want.
  • Helping Users Find Your Content on TV - With all the content choices at users' fingertips, helping them find content they enjoy is almost as important as providing that content. This training discusses how to surface your content on TV devices.
  • Building TV Games - TV devices are a great platform for games. See this topic for information on building great game experiences for TV.
  • Building Live TV Apps - Present your video content in a linear, "broadcast TV" style with channels and programs that your users can access through a program guide as well as the channel up/down buttons.

 》To create an virtual TV device:

  1. Start the AVD Manager. For more information, see the AVD Manager help.
  2. In the AVD Manager dialog, click the Device Definitions tab.
  3. Select one of the Android TV device definitions and click Create AVD.
  4. Select the emulator options and click OK to create the AVD.

Note: For best performance of the TV emulator device, enable the Use Host GPU option and, where supported, use virtual device acceleration.

To test your application on the virtual TV device:

  1. Compile your TV application in your development environment.
  2. Run the application from your development environment and choose the TV virtual device as the target.
 TV-Handling TV Hardware

 TV hardware is substantially different from other Android devices. TVs do not include some of the hardware features found on other Android devices, such as touch screens, cameras, and GPS receivers. TVs are also completely dependent on secondary hardware devices. In order for users to interact with TV apps, they must use a remote control or game pad. When you build an app for TV, you must carefully consider the hardware limitations and requirements of operating on TV hardware.

》The recommended way to determine if your app is running on a TV device is to use theUiModeManager.getCurrentModeType() method to check if the device is running in television mode. The following example code shows you how to check if your app is running on a TV device:

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")
}
》 to declare that your app does not require hardware features which are unavailable on TV devices, even though your app may use these features on non-TV devices:
<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"/>

Note: Some features have subfeatures like android.hardware.camera.front, as described in the Feature Reference. Be sure to mark as required="false" any subfeatures also used in your app.

 Caution: Declaring a hardware feature as required by setting its value to true prevents your app from being installed on TV devices or appearing in the Android TV home screen launcher.

Use the hasSystemFeature(String) method to check for specific features at runtime. 

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

》 TVs are stationary, indoor devices, and do not have built-in global positioning system (GPS) receivers. If your app uses location information, you can still allow users to search for a location, or use a static location provider such as a zip code configured during the TV device setup.

// Request a static location from the location manager
LocationManager locationManager = (LocationManager) this.getSystemService(
        Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation("static");

// Attempt to get postal or zip code from the static location object
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);
}
 The default controller for a TV device is a D-pad. In general, your app should be operable from a remote controller that only has up, down, left, right, select, Back, and Home buttons. If your app is a game that typically requires a game controller with additional controls, your app should attempt to allow gameplay with these D-pad controls. In this case, your app should also warn the user that a controller is required and allow them to exit your game gracefully using the D-pad controller.

 Controllers for TV are frequently Bluetooth devices which may attempt to save power by periodically going into sleep mode and disconnecting from the TV device. This means that an app might be interrupted or restarted if it is not configured to handle these reconnect events. These events can happen in any of the following circumstances:

  • While watching a video which is several minutes long, a D-Pad or game controller goes into sleep mode, disconnects from the TV device and then reconnects later on.
  • During gameplay, a new player joins the game using a game controller that is not currently connected.
  • During gameplay, a player leaves the game and disconnects a game controller.
 TV device users may have more than one type of controller that they use with their TV. For example, a user might have both a basic D-pad controller and a game controller.

相關文章