Android Direction sensor sensor.type_orientation after abandonment get direction information

Source: Internet
Author: User

Android Direction sensor Sensor.type_orientation has been in the wirelessly is not recommended to use, with getorientation to replace, with this but not as before, you can get the data, need to use both geomagnetic sensors and accelerometer to obtain, the code is as follows:

 PackageCom.catcher.testcompass;Importandroid.app.Activity;ImportAndroid.hardware.Sensor;Importandroid.hardware.SensorEvent;ImportAndroid.hardware.SensorEventListener;ImportAndroid.hardware.SensorManager;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public classFouractivityextendsActivity {PrivateTextView TV; PrivateSensormanager Manager; PrivateMysensoreventlistener Listener; PrivateSensor magneticsensor, accelerometersensor; Private float[] values, r, Gravity, geomagnetic; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_four); TV=(TextView) Findviewbyid (r.id.tv); //Get SensormanagerManager =(Sensormanager) Getsystemservice (Sensor_service); Listener=NewMysensoreventlistener (); //Get sensorMagneticsensor =manager.getdefaultsensor (Sensor.type_magnetic_field); Accelerometersensor=manager.getdefaultsensor (Sensor.type_accelerometer); //initializing an arrayValues =New float[3];//to save the final result.Gravity =New float[3];//the values used to hold the accelerometerR =New float[9];//Geomagnetic =New float[3];//used to hold the value of the geomagnetic sensor    }    Private classMysensoreventlistenerImplementsSensoreventlistener {@Override Public voidonsensorchanged (Sensorevent event) {if(Event.sensor.getType () = =Sensor.type_magnetic_field) {Geomagnetic=event.values; }            if(Event.sensor.getType () = =sensor.type_accelerometer) {Gravity=event.values;            GetValue (); }} @Override Public voidOnaccuracychanged (sensor sensor,intaccuracy) {}} @Overrideprotected voidOnresume () {Super. Onresume ();        Manager.registerlistener (Listener, Magneticsensor, sensormanager.sensor_delay_normal);    Manager.registerlistener (Listener, Accelerometersensor, sensormanager.sensor_delay_normal); } @Overrideprotected voidOnPause () {Manager.unregisterlistener (listener); Super. OnPause (); }     Public voidGetValue () {//R back from hereSensormanager.getrotationmatrix (R,NULL, gravity, geomagnetic); //values return from heresensormanager.getorientation (R, values); //Extracting Data        DoubleAzimuth = Math.todegrees (values[0]); if(azimuth<0) {Azimuth=azimuth+360; }        DoublePitch = Math.todegrees (values[1]); DoubleRoll = Math.todegrees (values[2]);        Tv.invalidate (); Tv.settext ("Azimuth:" + (int) Azimuth + "\npitch:" + (int) Pitch + "\nroll:" + (int); }}

Layout file has only one TextView

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Tools:context= "Com.catcher.testcompass.MainActivity" >    <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "20SP"Android:text= "@string/hello_world" /></Relativelayout>

Android Direction sensor sensor.type_orientation after abandonment get direction information

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.