Android sensors (ii) Electronic Compass

Source: Internet
Author: User

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 ));}}}};

 

 

Related Article

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.