Android Development-Sensor information acquisition

Source: Internet
Author: User

Table 1: Motion sensors supported by the Android platform.

Reference API Documentation: Http://developer.android.com/reference/android/hardware/Sensor.html#TYPE_GYROSCOPE

sensor Sensor event Data Description Unit of Measure
Type_accelerometer Sensorevent.values[0] Acceleration along the x-axis (including gravity). M/s2
SENSOREVENT.VALUES[1] Acceleration along the y axis (including gravity).
SENSOREVENT.VALUES[2] Acceleration along the z axis (including gravity).
Type_gravity Sensorevent.values[0] The gravitational acceleration along the x-axis. M/s2
SENSOREVENT.VALUES[1] The gravitational acceleration along the y-axis.
SENSOREVENT.VALUES[2] The gravitational acceleration along the z axis.
Type_gyroscope Sensorevent.values[0] The rotational angular velocity around the x-axis. rad/s
SENSOREVENT.VALUES[1] The rotational angular velocity around the y-axis.
SENSOREVENT.VALUES[2] The rotational angular velocity around the z-axis.
Type_linear_acceleration Sensorevent.values[0]] Acceleration along the x-axis (not including gravity). M/s2
SENSOREVENT.VALUES[1] Acceleration along the y-axis (not including gravity).
SENSOREVENT.VALUES[2] Acceleration along the z axis (not including gravity).
Type_rotation_vector Sensorevent.values[0]] Rotates the vector along the x-axis portion (x * sin (Θ/2)). No no No
SENSOREVENT.VALUES[1] Rotates the part of the vector along the y-axis (y * sin (Θ/2)).
SENSOREVENT.VALUES[2]] Rotates the part of the vector along the z-axis (z * sin (Θ/2)).
SENSOREVENT.VALUES[3]] The numerical portion of the rotation vector ((cos (Θ/2)) 1.

1The numeric part is optional.

In the Android2.3 gingerbread system, Google offers 11 types of sensor supply layer usage.

1 #defineSensor_type_accelerometer 1//Acceleration2 #defineSensor_type_magnetic_field 2//Magnetic3 #defineSensor_type_orientation 3//direction4 #defineSensor_type_gyroscope 4//Gyroscope5 #defineSensor_type_light 5//Light Sensing6 #defineSensor_type_pressure 6//pressure7 #defineSensor_type_temperature 7//Temperature8 #defineSensor_type_proximity 8//Close9 #defineSensor_type_gravity 9//GravityTen #defineSensor_type_linear_acceleration 10//Linear Acceleration One #defineSensor_type_rotation_vector 11//rotation Vector

Figure 1.Rotates the coordinate system of the vector sensor.

Key classes and Interfaces

    1. Sensor
    2. Sensorevent
    3. Sensormanager
    4. Sensoreventlistener

Developing sensors in Android is simple and requires three steps:

1, call the Context of the getsystemservice(Context.sensor_service) method to obtain a sensor management Service (Sensormanager).

2. Call Sensormanager's getdefaultsensor(int type) method to get the sensor of the specified type.

3, generally in the Onresume () method call Sensormanager registerlistener(sensoreventlistener listener,sensor sensor,int rate) Register the Listener. The three parameters of the method are described below:

Listener: Monitors listener for sensor events. The listener needs to implement the Sensoreventlistener interface.

Sensor: Sensors object.

Rate: Specifies how often to get sensor data. The following frequency values are supported:

The following is a demonstration of accelerometer applications, which are tested in both real and simulator ways, with slightly different approaches.

Real machine:

Activity:

[Java]View Plaincopy
  1. 1 Package Com.home; 2   3 import android.app.Activity; 4 import Android.content.Context; 5 import Android.hardware.Sensor; 6 import android.hardware.SensorEvent; 7 import Android.hardware.SensorEventListener; 8 import Android.hardware.SensorManager; 9 import Android.os.Bundle; Ten import Android.widget.TextView;  One    A  Public classAccelerometeractivity extends Activity implements - Sensoreventlistener { -     //define the system's sensor management services the     PrivateSensormanager Sensormanager;  -     PrivateTextView Showtext;  -    - @Override +     protected voidonCreate (Bundle savedinstancestate) { - super.oncreate (savedinstancestate);  + Setcontentview (R.layout.main);  AShowtext =(TextView) Findviewbyid (r.id.main_tv_show);  at         //get the system's sensor management Service -Sensormanager =(Sensormanager) Getsystemservice (Context.sensor_service);  -     }   -    - @Override -     protected voidOnresume () { in Super.onresume ();  -         //registering listeners for the system's accelerometer sensor toSensormanager.registerlistener ( This,   + Sensormanager.getdefaultsensor (sensor.type_accelerometer), - sensormanager.sensor_delay_game);  the     }   *    $ @OverridePanax Notoginseng     protected voidOnStop () { -         //Cancel Registration theSensormanager.unregisterlistener ( This);  + Super.onstop ();  A     }   the    +     //callback the method when the sensor's accuracy is changed - @Override $      Public voidOnaccuracychanged (sensor sensor,intaccuracy) {   $     }   -    -     //callback This method when the value of the sensor has changed the @Override -      Public voidOnsensorchanged (sensoreventEvent) {  Wuyi         if(Event. Sensor.gettype () = =sensor.type_accelerometer) {   the             float[] values =Event. Values;  -StringBuffer SB =NewStringBuffer ();  WuSb.append ("acceleration on the X-axis:"+ values[0] +"\ n");  -Sb.append ("acceleration on the y-axis:"+ values[1] +"\ n");  AboutSb.append ("acceleration on z-axis:"+ values[2] +"\ n");  $ Showtext.settext (sb.tostring ());  -         }   -     }   -    A}

The layout file has only one textview, which is omitted here.

Android Development-Sensor information acquisition

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.