Android provides support for device sensors, and as long as the Android device's hardware provides these sensors, Android apps can access the device's external conditions through sensors, including the phone's running status, the current orientation, and so on. The Android system also provides drivers to manage these sensor hardware, which can be used by listeners to monitor changes in the external environment perceived by the sensor hardware. This blog mainly explains how to use the sensor under Android, and finally a simple demo.
Developing sensor application Steps
Developing an application for sensor support is simple, as long as the developer assigns a listener to the sensor to be monitored in the Sensor manager Sensormanager, and when the external environment changes, the Android system obtains data from the external environment via the sensor. The data is then passed to the listener's listener callback method. The steps are as follows:
- Gets the sensor service.
- Gets the sensor of the specified type from the sensor service.
- Add a sensor's listener using the sensor service.
- After you are done, log off the sensor's listener.
Get sensor service
There are many system-level services built into Android that are used for developers, and sensors are managed through sensor services, Sensormanager. Instead, get the system service in the Android component, use the method Context.getsystemservice (String), and its parameters are defined in the context in static final mode. The acquisition of Sensormanager requires an incoming context.sensor_service.
Manager= (Sensormanager) Getsystemservice (Sensor_service);
Obtaining a sensor of the specified type from the sensor service
The sensor service manages all the sensors on the device, so you need to specify the sensor to be monitored. To get the sensor to be monitored, you need to use the Sensormanager.getdefaultsensor () method, and its full signature is as follows:
Sensor getdefaultsensor (int type)
Sensors in Android require sensor support, and the Getdefaultsensor () method obtains the corresponding sensor from the specified type parameter. The type parameter is defined in a static final way inside the sensor, allowing developers to use it directly. The following describes the type of several common sensors:
- Sensor.type_orientation: direction sensor.
- Sensor.type_accelerometer: Gravity sensor.
- Sensor.type_light: Light sensor.
- Sensor.type_magnetic_field: magnetic field sensor.
To add a sensor's listener using the sensor service
Once you have obtained the Sensormanager and sensor objects, you can register the listener for their sensor. Registering the listener for the sensor, using the Sensormanager.registerlistener () method, has multiple overloaded methods, but some methods are obsolete, and the signature of a common method is provided below:
Boolean Registerlistener (Sensoreventlistener listener,sensor sensor,int rateus)
The meaning of the above method parameter: Listener: Sensor's listener, sensors: sensor to be monitored, rateus: sample rate of the sensor.
As can be seen from the Registerlistener () method, it needs to pass a Sensoreventlistener object, which is the sensor listener, which contains two methods that need to be implemented by the developer:
- void onaccuracychanged (sensor sensor,int accuracy): callback when sensor accuracy is changed.
- void Onsensorchanged (Sensorevent event): Callback when the sensor-induced value changes.
For the above two methods, the accuracy of the sensor is generally not changed, so we generally the main amount of code in onsensorchanged ().
The Registerlistener () method also has a Rateus parameter, which indicates the sampling rate of the monitor sensor change, which is the frequency at which the value is obtained from the sensor. It is defined in the form of static final defined in Sensormanager, which is convenient for us to use directly, and defines several options as follows:
- Sensormanager.sensor_delay_fastest: Fastest, least latency.
- Sensormanager.sensor_delay_game: The frequency for the game.
- Sensormanager.sensor_delay_normal: Normal frequency.
- SENSORMANAGER.SENSOR_DELAY_UI: The frequency of UI changes for normal user interfaces.
Android provides us with these sample rate parameters that are convenient for us to use. But for the choice of that sampling rate, not the faster the better, to refer to the actual development of the application situation, the larger the sampling rate, the more resources, including the power, CPU, etc., so to choose according to the actual situation, after all, the powerful application, if the device can reduce the battery life, it will be users do not like.
After use, log off the sensor's listener
After the sensor is used, the listener needs to be unregistered because the sensor's listener does not release the resource itself due to the end of the application, requiring the developer to log out at the appropriate time. The logoff sensor listener uses the Sensormanager.unregisterlistener () method, and, like the listener's registration method, it has several overloaded methods, but some are deprecated, and a common full signature is described below:
void Unregisterlistener (Sensoreventlistener listener)