Advanced Article-Android system: 4. Android mobile phone action sensor

Source: Internet
Author: User

Motion sensors are useful for monitoring the movement of devices, such as tilt, vibration, rotation, and swing, which are the monitoring ranges of motion sensors. The movement of a device is usually a direct response to user input.

All motion sensors return a value of three floating-point numbers, and for different sensors, the three values have different meanings. For example, for an accelerometer, the acceleration data for three axes is returned. For a gyroscope sensor, the rotational angular velocity of three coordinate cycles is returned.

Use of motion sensors and data return: acceleration sensor

If you simply use the accelerometer to return data, you will find that the acceleration of the z axis is more than 9.8. So it seems that Android is the static cell phone vertical acceleration default to the acceleration of gravity, and the XY axis data is not very accurate. This is not the effect we want. It is therefore necessary to use the data of the gravity sensor to adjust the accelerated sensor to make its data more accurate.

ImportAndroid.hardware.Sensor;Importandroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;Importjava.util.List; Public classMainactivityextendsAppcompatactivityImplementssensoreventlistener{PrivateSensormanager Msensormanager; Private float[] Gravity =New float[3]; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Msensormanager=(Sensormanager) Getsystemservice (Sensor_service); } @Override Public voidOnsensorchanged (Sensorevent sensorevent) {//This method is called when the sensor accuracy changed        Switch(SensorEvent.sensor.getType ()) { CaseSensor.type_accelerometer:Final floatAlpha = (float) 0.8; gravity[0] = Alpha * Gravity[0] + (1-alpha) * sensorevent.values[0]; gravity[1] = Alpha * Gravity[1] + (1-alpha) * sensorevent.values[1]; gravity[2] = Alpha * gravity[2] + (1-alpha) * sensorevent.values[2]; String Data= "acceleration:\n" + "X:" + (Sensorevent.values[0]-gravity[0]) + "\ n" + "Y:" + (Sensorevent.values[1]-gravity[1]) + "\ n" + " Z: "+ (Sensorevent.values[2]-gravity[2]) +" \ n ";                SYSTEM.OUT.PRINTLN (data); //System.out.println (sensorevent.values[2]-gravity[2]);                 Break;  Casesensor.type_gravity:gravity[0]=sensorevent.values[0]; gravity[1]=sensorevent.values[1]; gravity[2]=sensorevent.values[2];  Break; }} @Override Public voidOnaccuracychanged (sensor sensor,inti) {//This method is called when the sensor data changed} @Overrideprotected voidOnresume () {Super. Onresume (); Msensormanager.registerlistener ( This, Msensormanager.getdefaultsensor (sensor.type_accelerometer), sensormanager.sensor_delay_ui); Msensormanager.registerlistener ( This, Msensormanager.getdefaultsensor (sensor.type_gravity), sensormanager.sensor_delay_fastest); } @Overrideprotected voidOnPause () {Super. OnPause (); Msensormanager.unregisterlistener ( This); }}

We register the sensor in the Onresume method. The sensor is de-registered in the OnPause method.

The video does not say why the processing of data can reduce the error. I haven't seen it for a long while,--!. So this note will write down the method of reducing the error.

Output Result:

07-15 20:49:12.663 28389-28389/bhu.com.myapplication i/system.out:acceleration:07-15 20:49:12.673 28389-28389/ Bhu.com.myapplication i/system.out:x:0.01028454307-15 20:49:12.683 28389-28389/bhu.com.myapplication I/System.out: Y:7.79517e-407-15 20:49:12.683 28389-28389/bhu.com.myapplication i/system.out:z:0.010925293
07-15 20:49:12.723 28389-28389/bhu.com.myapplication i/system.out:acceleration:07-15 20:49:12.733 28389-28389/ Bhu.com.myapplication i/system.out:x:-0.1063993507-15 20:49:12.733 28389-28389/bhu.com.myapplication I/System.out: Y:0.01779899407-15 20:49:12.733 28389-28389/bhu.com.myapplication i/system.out:z:0.018583298
07-15 20:49:12.793 28389-28389/bhu.com.myapplication i/system.out:acceleration:07-15 20:49:12.793 28389-28389/ Bhu.com.myapplication i/system.out:x:0.0485320107-15 20:49:12.793 28389-28389/bhu.com.myapplication i/system.out:y : -0.0327197707-15 20:49:12.793 28389-28389/bhu.com.myapplication i/system.out:z:0.04224682

It seems that the accelerometer is still very sensitive. Tiny vibrations can also be measured accurately.

Advanced Article-Android system: 4. Android mobile phone action sensor

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.