Acceleration Sensor Accelerometer

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.