About the use of the Android orientation sensor

Source: Internet
Author: User

After Android2.2, orientation sensors was deprecated.

The official proposal is to use acceleration and magnetic sensor to calculate

On this question, csdn on a very good blog post.

http://blog.csdn.net/flowingflying/article/details/43233315

On stack Overflow:

Http://stackoverflow.com/questions/10291322/what-is-the-alternative-to-android-orientation-sensor

Use the words to see the code directly

 Packagecom.turtle920.androidaudioprocess;Importandroid.app.Activity;ImportAndroid.hardware.Sensor;Importandroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.widget.TextView;//implement sensor Event monitoring: Sensoreventlistener Public classMainactivityextendsActivityImplementssensoreventlistener{PrivateSensormanager Sensormanager; PrivateSensor Acc_sensor; PrivateSensor Mag_sensor; //Acceleration Sensor Data    floataccvalues[]=New float[3]; //Geomagnetic sensor data    floatmagvalues[]=New float[3]; //rotation matrix to hold the data of the magnetic field and acceleration    floatr[]=New float[9]; //data for the analog direction sensor (the original data is in radians)    floatvalues[]=New float[3]; intCounter=0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Sensormanager=(Sensormanager) Getsystemservice (Sensor_service); Acc_sensor=sensormanager.getdefaultsensor (Sensor.type_accelerometer); Mag_sensor=sensormanager.getdefaultsensor (Sensor.type_magnetic_field); //registering the sensor for monitoring:Sensormanager.registerlistener ( This, Acc_sensor, sensormanager.sensor_delay_normal); Sensormanager.registerlistener ( This, Mag_sensor, sensormanager.sensor_delay_normal); }    //callback method when the sensor state changes@Override Public voidonsensorchanged (Sensorevent event) {if(Event.sensor.getType () = =sensor.type_accelerometer) {Accvalues=event.values; }        Else if(Event.sensor.getType () = =Sensor.type_magnetic_field) {Magvalues=event.values; }        /**Public Static Boolean Getrotationmatrix (float[] r, float[] I, float[] gravity, float[] geomagnetic) * Fill rotation array R * r: Rotating array to fill * I: convert magnetic field data into actual gravity coordinates can be set to NULL by default * Gravity: Accelerometer data * Geomagnetic: Geomagnetic sensor Data*/Sensormanager.getrotationmatrix (R,NULL, Accvalues, magvalues); /*** public static float[] Getorientation (float[] r, float[] values) * R: Rotating Array * Values: Analog direction sensor of data*/sensormanager.getorientation (R, values); if(counter++% 10==1) {LOG.E ("DEBUG", "x:" +math.todegrees (Values[0]) + "Y:" +math.todegrees (values[1]) + "Z:" +math.todegrees (values[2])); }} @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) {    }}

The output of x, Y, z three values is the rotation angle of the phone along this axis

One of the things to be aware of in this place is that the magnetic force of the MacBook's screen-opening magnets will affect the magnetic sensor ... Be careful when you debug.

About the use of the Android orientation 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.