Acceleration Sensor Accelerometer
The Android system supports powerful management services for sensors. The steps for developing sensor applications are as follows:
(1) Call the getSystemService (Context. SENSOR_SERVICE) method of Context to obtain the Sensor Manager object. The SensorManager object represents the system's Sensor management service.
(2) Call the getdefasensensor (int type) method of SensorManager to obtain the sensor of the specified type.
(3) Generally, call the registerListener () of SensorManager in the onResume () method of the Activity to register the listener for the specified sensor.
Accelerometer. java
Public class AccelerometerTest extends Activity implements SensorEventListener {SensorManager sensorManager; EditText etTxt1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); etTxt1 = (EditText) findViewById(R.id.txt 1); // get the Sensor Management Service sensorManager = (SensorManager) getSystemService (Context. SENSOR_SERVICE) ;}@ Overrideprotected v Oid onResume () {super. onResume (); // register the listener sensorManager for the system's accelerometer. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), sensorManager. SENSOR_DELAY_GAME) ;}@ Overrideprotected void onStop () {// cancel sensorManager registration. unregisterListener (this); super. onStop ();} // call back this method when the sensor precision changes. @ Overridepublic void onAccuracyChanged (Sensor arg0, int arg1) {// TODO Auto-generated method stub} // The following method is required to implement the SensorEventListener interface // call back this method when the sensor value changes @ Overridepublic void onSensorChanged (SensorEvent event) {float [] values = event. values; StringBuilder sb = new StringBuilder (); sb. append ("acceleration in the X direction:"); sb. append (values [0]); sb. append ("\ nY acceleration:"); sb. append (values [1]); sb. append ("\ nZ acceleration:"); sb. append (values [2]); etTxt1.setText (sb. toString ());}}
The sensor coordinate system is different from the screen coordinate system. The X axis of the sensor coordinate system is left along the screen, the Y axis is up along the screen, and the Z axis is perpendicular to the screen.