Android sensors can monitor the environment everywhere
Such as: direction, acceleration table, light, magnetic field, closeness, temperature, etc.
Android. Hardware. sensormanager contains several constants, which indicate different aspects of the android sensor system, including:
Sensor Type direction, acceleration table, light, magnetic field, closeness, temperature, etc. The fastest sampling rate, game, common, and user interface. When an application requests a specific sampling rate, it only prompts or recommends the sensor subsystem. The specified sampling rate is not guaranteed. High Accuracy, Low, Medium, and unreliable.
The sensorlistener interface is the center of the sensor application. It includes two required methods:
The onsensorchanged (INT sensor, float values []) method is called when the sensor value is changed. This method is only called by the sensor monitored by this application (for more information, see the following ). Parameters of this method include: an integer indicating the changed sensor; an array of floating-point values indicating the sensor data itself. Some sensors only provide one data value, while others provide three floating point values. The direction sensor and the acceleration table sensor both provide three data values.
When the sensor's accuracy changes, the onaccuracychanged (INT sensor, int accuracy) method is called. Parameters include two integers: one representing the sensor and the other representing the new accurate value of the sensor.
To interact with a sensor, the application must register to listen for activities related to one or more sensors. Register and use the registerlistener method of the sensormanager class. The sample code in this article demonstrates how to register and deregister the sensorlistener.
Example 1:
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. widget. textview;
Import Android. Hardware. sensormanager;
Import Android. Hardware. sensorlistener;
Public class ibmeyes extends activity implements sensorlistener {
Final string tag = "ibmeyes ";
Sensormanager Sm = NULL;
Textview xviewa = NULL;
Textview yviewa = NULL;
Textview zviewa = NULL;
Textview xviewo = NULL;
Textview yviewo = NULL;
Textview zviewo = NULL;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Get reference to sensormanager
Sm = (sensormanager) getsystemservice (sensor_service );
Setcontentview (R. layout. Main );
Xviewa = (textview) findviewbyid (R. Id. Xbox );
Yviewa = (textview) findviewbyid (R. Id. ybox );
Zviewa = (textview) findviewbyid (R. Id. zbox );
Xviewo = (textview) findviewbyid (R. Id. xboxo );
Yviewo = (textview) findviewbyid (R. Id. yboxo );
Zviewo = (textview) findviewbyid (R. Id. zboxo );
}
Public void onsensorchanged (INT sensor, float [] values ){
Synchronized (this ){
Log. D (TAG, "onsensorchanged:" + sensor + ", X:" +
Values [0] + ", Y:" + values [1] + ", Z:" + values [2]);
If (sensor = sensormanager. sensor_orientation ){
Xviewo. settext ("orientation X:" + values [0]);
Yviewo. settext ("orientation Y:" + values [1]);
Zviewo. settext ("orientation Z:" + values [2]);
}
If (sensor = sensormanager. sensor_accelerometer ){
Xviewa. settext ("accel X:" + values [0]);
Yviewa. settext ("accel Y:" + values [1]);
Zviewa. settext ("accel Z:" + values [2]);
}
}
}
Public void onaccuracychanged (INT sensor, int accuracy ){
Log. D (TAG, "onaccuracychanged:" + sensor + ", accuracy:" + accuracy );
}
@ Override
Protected void onresume (){
Super. onresume ();
// Register this class as a listener for the orientation and accelerometer Sensors
SM. registerlistener (this,
Sensormanager. sensor_orientation | sensormanager. sensor_accelerometer,
Sensormanager. sensor_delay_normal );
}
@ Override
Protected void onpause ()(){
// Unregister listener
SM. unregisterlistener (this );
Super. onpause ()();
}
}
The oncreate method of this activity can reference sensormanager, which contains all functions related to the sensor.
The oncreate method also creates references to six textview Widgets. You need to update these widgets using sensor data values.
The onresume () method uses the reference to sensormanager to register Sensor updates using the registerlistener method:
The first parameter is an instance of the class that implements the sensorlistener interface.
The second parameter is the bit mask of the sensor. In this example, the application requests data from sensor_orientation and sensor_accelerometer.
The third parameter is a system prompt indicating the speed required by the application to update the sensor value.
After the application (activity) is paused, You need to log out of the listener so that no Sensor updates will be received later.
This is achieved through the unregisterlistener method of sensormanager. The only parameter is the instance of sensorlistener.
In the call of the registerlistener and unregisterlistener methods, the application uses the keyword "this.
Note the implements keyword in the class definition, which declares the class to implement the sensorlistener interface. This is why you want to pass it to registerlistener and unregisterlistener.
The sensorlistener must implement two methods: onsensorchange and onaccuracychanged.
The sample application does not care about the accuracy of the sensor, but the current x, y, and z values of the sensor.
The onaccuracychanged method does not execute any operations. It only adds one log entry for each call.
It seems that the onsensorchanged method is often needed because the acceleration table and the direction sensor are sending data quickly.
Check the first parameter to determine which sensor is sending data. After confirming the sensor that sent the data,
Update the corresponding UI element based on the data contained in the floating point value array passed by the second parameter.
Note: When you do not need to monitor the sensor, you must cancel the monitoring in time.