Process different sensor configurations
Android does not specify standard sensor configurations for devices, which means that the manufacturer can include any sensor configurations they want into their Android devices. Therefore, devices can contain a wide range of sensors. For example, Motorola's Xoom has a pressure sensor, but Samsung's Nexus S does not. Both Xoom and Nexus S have gyroscope, but HTC's Nexus One does not. If your application depends on a special type of sensor, you must ensure that the sensor exists on the device to ensure that your application can run successfully. There are two ways to determine whether a specified sensor exists on the device:
1. Detect sensors during operation and enable or disable application-related functions based on detection results;
2. Use Google Play to filter out the specified sensor configurations on the target device.
Detect sensors during operation
If your application uses a special sensor type but does not want to rely on it, you can use the sensor framework to detect the sensor at runtime, then you decide to disable or enable the corresponding functions of the application. For example, a navigation application may use temperature sensors, pressure sensors, GPS sensors, and geomagnetic sensors to show the temperature, pressure, position, and compass orientation. If the device does not have a pressure sensor, you can use the sensor framework to detect the presence of the pressure sensor at runtime and disable the UI part of the application that displays the pressure. For example, run the following code to check whether a pressure sensor exists on the device:
Private SensorManager mSensorManager;
...
MSensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE );
If (mSensorManager. getdefasensensor (Sensor. TYPE_PRESSURE )! = Null ){
// Success! There's a pressure sensor.
}
Else {
// Failure! No pressure sensor.
}
Use Google Play to filter sensor configurations specified by the target
If you want to publish an application to Google Play, you can use the <uses-feature> element in your application list file to filter devices without the corresponding sensor configuration. The <uses-feature> element has several hardware descriptors that allow you to filter applications based on the existence of specified sensors. The sensors you can list include: acceleration, pressure, COMPASS (geomagnetic field), gyroscope, brightness, and distance. The following application list prevents the application from being installed on a device without an accelerometer:
<Uses-feature android: name = "android. hardware. sensor. accelerometer"
Android: required = "true"/>
If you add this element to the Application List, on Google Play, users will only see your application on devices with accelerometer.
If your application depends entirely on a specific sensor, you should set the description to: android: required = "true ". If your application only uses some features to use the sensor, and it still runs when no sensor is available, you should list the sensor in the <uses-feature> element, however, you must set the descriptor to android: required = "false ". This will help you install your application even On devices with no sensor specified. This is also the best practice of project management. It helps you track the usage of application functions. Remember, if your application uses a special sensor but still runs when no sensor is available, you should detect the sensor at run time, disable or enable the corresponding functions of the application based on the detection results.