Recipe 9.2 Detecting the Presence of Hardware Features

Android Versions
Level 1 and above (unless otherwise indicated in code)
Permissions
None
Source Code to Download from Wrox.com
DetectHardwareFeatures.zip

One great advantage of the Android platform is the wide variety of devices from which users can select. However, with choice comes fragmentation — devices in all shapes and sizes, each supporting different sets of features. This fragmentation problem causes a lot of headaches for developers, who have to ensure that their application runs on the widest range of devices available. This recipe demonstrates how to determine whether a particular hardware feature is supported on the device.

Solution

Several methods enable you to detect hardware features on your devices:

  • getResources() — This method returns an instance of the Resources class for your package. Using this instance, you can use the getConfiguration() method to get the current configuration for your package. This technique enables you to find information such as whether a hardware keyboard is attached to your device, whether the device supports landscape orientation, and so on.
  • ViewConfiguration.get() — This method returns a ViewConfiguration object that enables you to check whether a device has a hardware MENU key, and other information such as the scrollbar size, the time needed for a long press gesture, and more.
  • getPackageManager() — This method returns a PackageManager object that enables you ...

Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.