Electronic Compass:
1./* Get sensormanager */
Sensormanager msensormanager = (sensormanager) getsystemservice (sensor_service );
2./* obtain the required sensor and register the sensoreventlistener */
Msensormanager. registerlistener (msensoreventlistener, msensormanager. getdefasensensor (sensor. type_orientation), sensormanager. sensor_delay_normal );
3./* implement the listener and override the Method */
Private Final sensoreventlistener msensoreventlistener = new sensoreventlistener ()
{Public void onaccuracychanged (sensor, int accuracy ){}
Public void onsensorchanged (sensorevent event ){
/* Determine the sensor type */
If (event. sensor. GetType () = sensor. type_orientation ){
/* Obtain X-value data
* X_data is a horizontal value.
* Because it is divided into eight directions, each direction is 45 °.
**/
Float x_data = event. Values [sensormanager. data_x];
// 337.5 ° to 22.5 ° north
If (x_data> 0 & amp; x_data <= 22.5) | x_data> 337.5 ){
Textview01.settext ("Northern" + String. valueof (x_data ));}
// The Northeast is 22.5 ° to 67.5 °
Else if (x_data> 22.5 & x_data <= 67.5 ){
Textview01.settext ("North East" + String. valueof (x_data ));}
// The East is 67.5 ° to 112.5 °
Else if (x_data> 67.5 & x_data <= 112.5 ){
Textview01.settext ("oriental" + String. valueof (x_data ));}
// The southeast direction is 112.5 ° to 157.5 °
Else if (x_data> 112.5 & x_data <= 157.5 ){
Textview01.settext ("east south" + String. valueof (x_data ));}
// The south is 157.5 ° to 202.5 °
Else if (x_data> 157.5 & x_data <= 202.5 ){
Textview01.settext ("Southern" + String. valueof (x_data ));}
// The Southwest China is 202.5 ° to 247.5 °
Else if (x_data> 202.5 & x_data <= 247.5 ){
Textview01.settext ("Southern West" + String. valueof (x_data ));}
// The western region ranges from 247.5 ° to 292.5 °.
Else if (x_data> 247.5 & x_data <= 292.5 ){
Textview01.settext ("Western" + String. valueof (x_data ));}
// The northwest side is 292.5 ° to 337.5 °
Else if (x_data> 292.5 & x_data <= 337.5 ){
Textview01.settext ("North West" + String. valueof (x_data ));}}}};