In Android, all sensors must be accessed through SensorMannager,
Call the getSystemService (SENSOR_SERVICE) method to obtain the sensor management object of the mobile phone,
OnSensorChanged (SensorEvent se) is called when the sensor value is changed,
To use a Sensor, you must call the registerListener (SensorEventListener listener, sensor Sensor, int rate) method for registration,
The sensor has three float values: x, y, and z. The value ranges from-10 to 10,
X = 0, y = 0, z = 10
When the mobile phone screen is placed horizontally down (z axis toward the ground), x = 0, y = 0, z =-10
X = 10, y = 0, z = 0
When the mobile phone is vertical (y axis to day), x = 0, y = 10, z = 0
Code:
PS: There is a TextView in the XML file to display the value of the gravity sensor,
[Java] public class TestActivity extends Activity {
TextView textView;
Float x, y, z;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView = (TextView) findViewById (R. id. textView );
// Obtain the gravity sensor hardware controller
SensorManager sm = (SensorManager) this. getSystemService (SENSOR_SERVICE );
Sensor sensor = sm. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
// Add a gravity sensing listener and implement the method,
SensorEventListener sel = new SensorEventListener (){
Public void onSensorChanged (SensorEvent se ){
X = se. values [SensorManager. DATA_X];
Y = se. values [SensorManager. DATA_Y];
Z = se. values [SensorManager. DATA_Z];
TextView. setText ("x =" + (int) x + "y =" + (int) y + "z =" + (int) z );
}
Public void onAccuracyChanged (Sensor arg0, int arg1 ){
}
};
// Register Listener. SENSOR_DELAY_GAME indicates the detection accuracy,
Sm. registerListener (sel, sensor, SensorManager. SENSOR_DELAY_GAME );
}
Public class TestActivity extends Activity {
TextView textView;
Float x, y, z;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
TextView = (TextView) findViewById (R. id. textView );
// Obtain the gravity sensor hardware controller
SensorManager sm = (SensorManager) this. getSystemService (SENSOR_SERVICE );
Sensor sensor = sm. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
// Add a gravity sensing listener and implement the method,
SensorEventListener sel = new SensorEventListener (){
Public void onSensorChanged (SensorEvent se ){
X = se. values [SensorManager. DATA_X];
Y = se. values [SensorManager. DATA_Y];
Z = se. values [SensorManager. DATA_Z];
TextView. setText ("x =" + (int) x + "y =" + (int) y + "z =" + (int) z );
}
Public void onAccuracyChanged (Sensor arg0, int arg1 ){
}
};
// Register Listener. SENSOR_DELAY_GAME indicates the detection accuracy,
Sm. registerListener (sel, sensor, SensorManager. SENSOR_DELAY_GAME );
}
Of course, you can also use the SensorEventListener interface,
Sensors cannot be tested in the simulator. The following is the test result of a real machine,
From Hu HU's column