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