Android Motion sensor

Source: Internet
Author: User

Android Motion sensor

The Android platform supports a number of sensors for monitoring device actions (5 sensors in total). Two of them (acceleration sensor and gyroscope sensor) are pure hardware sensors. The other three (gravity sensors, linear acceleration sensors, and rotational vector sensors) may be hardware sensors or software sensors. For example, in some Android devices, these software-based sensors get data from acceleration and magnetic sensors, but they may also get data from gyroscope sensors on other Android devices. In other words, the same software-based sensor may return data from different hardware sensors on different Android devices. So the same sensor based on software may have different accuracy and range of use in various devices. Most high-end Android devices will have acceleration sensors and some have gyroscope sensors.

Motion sensors are useful for monitoring the movement of devices, such as tilt, vibration, rotation, and swing, which are the monitoring ranges of the motion sensors. The movement of a device is usually a direct response to user input. For example, the user is racing in the game, or controlling a ball in the game). In addition, the physical environment in which the device is located is also reflected in the action of the device, for example, when the user is driving a car, and this is an Android device that is lying quietly in the seat next to it, even though the device is not moving, but will vibrate as the car moves and the device will move with the car.
For the first case, the relative position of the device itself can be monitored. For the second case, a reference frame other than the device needs to be taken into account. The motion sensor itself is not normally used to monitor the location of the device, and the location of the device needs to be monitored with other types of sensors, such as magnetic field sensors.

All motion sensors return a value of three floating-point numbers (returned by an array of length 3), but for different sensors, the three have a different meaning. For example, for an acceleration sensor, three axes of data are returned. For a gyroscope sensor, the rotational angular velocity of three axes is returned.

The device pushes from left to right and the x-axis acceleration is positive.

The device pushes toward itself, and the y-axis acceleration is positive.

If the acceleration of a m/s^2 is driven towards the sky, then the acceleration of the z-axis is a + 9.81, so if the actual acceleration is calculated (to counteract the gravitational acceleration), it needs to be reduced by 9.81.


<linearlayout xmlns:android= ""     xmlns:tools= "http// "android:layout_width=" match_parent "    android:layout_height=" Match_parent " android:paddingleft= "@dimen/activity_horizontal_margin"     android:paddingright= "@dimen/activity_ Horizontal_margin "    android:paddingtop=" @dimen/activity_vertical_margin "    Android: paddingbottom= "@dimen/activity_vertical_margin" tools:context= ". Mainactivity "    android:orientation=" vertical ">    <textview android:id=" @+id/sensor " Android:layout_width= "Wrap_content"         android:layout_height= "Wrap_content"/>    <textview android:id= "@+id/gravity" android:layout_width= "wrap_content"         Android: layout_height= "Wrap_content"/>    <textview android:id= "@+id/proximity1" android:layout_width= " Wrap_content "        Android:layout_height= "Wrap_content"/></linearlayout> 


Pupublic class Mainactivity extends actionbaractivity  implements sensoreventlistener{    Private TextView sensor=null;    Private Sensormanager msensormanager;    private TextView gravitytv=null;& nbsp   Private float[] gravity=new float[3];    @Override     protected void onCreate (Bundle savedinst Ancestate) {        super.oncreate (savedinstancestate);        Setcontentview ( R.layout.activity_main);        sensor= (TextView) Findviewbyid (;      & nbsp Msensormanager= (Sensormanager) getsystemservice (sensor_service);   }    @Override     protected void Onresume () {        Super.onresume ();        Msensormanager.regist Erlistener (this,                Msensormanager.getdefaultsensor (sensor.type_ ACCELEROMETER),//acceleration sensor,      &nbsp         SENSORMANAGER.SENSOR_DELAY_UI)//sensormanager.sensor_delay_ui sampling frequency         Msensormanager.registerlistener (this,                Msensormanager.getdefaultsensor (sensor.type_gravity),//Gravity sensor,                Sensormanager.sensor_delay_fastest)//sensormanager.sensor_delay_ui sampling frequency         Msensormanager.registerlistener (this,                Msensormanager.getdefaultsensor (sensor.type_proximity),//Proximity sensor                 SENSORMANAGER.SENSOR_DELAY_UI);   }    @Override     protected void OnPause () {  & nbsp     Super.onpause ();        Msensormanager.unregisterlistener (this);//Uninstall All sensors    }   //change of sensing status     @Override     public void onsensorchanged (Sensorevent evenT) {        switch (Event.sensor.getType ()) {            Case Sensor.type _accelerometer://Accelerometer                //minus noise (interference)         &NBSP ;       Final float alpha= (float) 0.8;                Gravity[0]=alpha *gravity[0]+ (1-alpha) *event.values[0];                gravity[1]=alpha*gravity [1]+ (1-alpha) *event.values[1];                gravity[2]=alpha*gravity[2]+ (1- Alpha) *event.values[2];                String accelerometer=string.valueof ("X:" + (Event.values[0]-gravity[0]) + "\ny:" +                        ( EVENT.VALUES[1]-GRAVITY[1]) + "\NZ:" +                        (EV ENT.VALUES[2]-GRAVITY[2]);                sensor= (TextView) Findviewbyid (;                Sensor.settext (accelerometer);                LOG.I (" X: ", String.valueof ((Event.values[0]-gravity[0]));                LOG.I (" Y: ", S Tring.valueof ((Event.values[1]-gravity[1]));                LOG.I ("Z:", String . ValueOf ((event.values[2]-gravity[2)));                break;    & nbsp       Case sensor.type_gravity:                Gravity[0]=event.val ues[0];                gravity[1]=event.values[1];        &NBS P       gravity[2]=event.values[2];                gravitytv= (TEXTVI EW) Findviewbyid ( Gravity);                Gravitytv.settext (string.valueof ("gravity[0]=" + event.values[0]+                "\ngravity[1]=" +event.values[1]+    & nbsp           "\ngravity[2]=" +event.values[2]);              &NBS P break;            case sensor.type_proximity:            &NBSP ;   TextView proximity= (TextView) Findviewbyid (;              &NBS P Proximity.settext (string.valueof (event.values[0));                break;        }   }   //accuracy change     @Override     public void ONACCU Racychanged (sensor sensor, int accuracy) {   }}

Android Motion sensor

Related Article

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: 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.