Anroid uses a direction sensor or an acceleration sensor to operate in six directions on a mobile phone or tablet

Source: Internet
Author: User

In the recent project, to achieve the mobile operation of Android devices in six directions, the first thing that comes to mind is the direction sensor. However, due to the diversity of Android devices, in the testing process, not all devices have direction sensors. If there is no direction sensor, acceleration sensors are used, but one drawback is that when horizontal movement occurs, there will be an error. The second step is that the project requires a horizontal screen, and the first direction of the mobile phone is different from that of the tablet. By default, the square direction of the mobile phone is 0, currently, the horizontal screen orientation is 1, while the flat panel is the horizontal screen by default, so the positive direction of the horizontal screen is 0.

For

private float accDefaultX = 9999;private float accDefaultY = 9999;private float fBaseDefault = 9999;

Is used to determine the initial direction, the program is based on the direction of the Enable sensor for the horizontal direction, if you need to use the actual horizontal direction for the horizontal direction of the device, you can remove

if (accDefaultX == fBaseDefault) {accDefaultX = fValues[0];}if (accDefaultY == fBaseDefault) {accDefaultY = fValues[1];}mx = fValues[0] - accDefaultX;my = fValues[1] - accDefaultY;

The source code is as follows:

Package COM. zhangjie. sensor; import android. app. activity; import android. content. context; import android. hardware. sensor; import android. hardware. sensorevent; import android. hardware. sensoreventlistener; import android. hardware. sensormanager; import android. OS. bundle; import android. util. log; import android. view. display; import android. view. view; import android. view. view. onclicklistener; import android. view. wi Ndowmanager; import android. widget. button; public class originsensor extends activity implements sensoreventlistener {public static final string tag = "originsensor"; private sensormanager msensormanager; private sensor ASensor; private windowmanager mwindowmanager; private display mdisplay; private int orientation = 0; private float accdefaultx = 9999; private float accdefaulty = 9999; private float fbasede Fault = 9999; private double angle = 25; private float [] fvalues = NULL; public static int sensor_type =-1; float MX, my; button clickbutton; Boolean iscleck = false; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); mwindowmanager = (windowmanager) getsystemservice (context. window_service); mdisplay = mwindowmanager. getdefadisplay display (); orientation = mdispla Y. getorientation (); msensormanager = (sensormanager) getsystemservice (context. sensor_service); clickbutton = new button (this); clickbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {If (! Iscleck) {enablesensor (); iscleck = true;} else {disablesensor (); iscleck = false ;}}); setcontentview (clickbutton);} public void enablesensor () {If (sensor_type =-1) {selectsensortype (); If (sensor_type = 2) {// shielding function} else {ASensor = msensormanager. getdefasensensor (sensor_type); msensormanager. registerlistener (this, ASensor, sensormanager. sensor_delay_ui);} accdefaultx = fbasedefault; // The reason for this is that the restart location is As the horizontal location accdefaulty = fbasedefault;} public void disablesensor () {msensormanager. unregisterlistener (this); current_dir = 0;}/** select the direction sensor first. If there is no direction sensor, select the acceleration sensor, if no acceleration sensor is available, the gravity sensing function * sensor_type = 3: Direction sensor * sensor_type = 1: acceleration sensor * sensor_type = 2: blocked gravity sensing function */Public void selectsensortype () {ASensor = msensormanager. getdefasensensor (sensor. type_orientation); Boolean OK = msensormanager. registerlistener (this, ASensor, sensormanager. sensor_delay_ui); If (OK) {sensor_type = sensor. type_orientation; angle = 25; log. E (TAG, "enable direction sensor");} else {ASensor = msensormanager. getdefasensensor (sensor. type_accelerometer); Boolean ok2 = msensormanager. registerlistener (this, ASensor, sensormanager. sensor_delay_ui); If (ok2) {sensor_type = sensor. type_accelerometer; angle = 2.5; log. E (TAG, "enable accelerometer");} else {sensor_type = 2; log. E (TAG, "") ;}}@ overridepublic void onsensorchanged (sensorevent event) {If (sensor_type = sensor. type_accelerometer) {fvalues = event. values; If (accdefaultx = fbasedefault) {accdefaultx = fvalues [0];} If (accdefaulty = fbasedefault) {accdefaulty = fvalues [1];} MX = fvalues [0]-accdefaultx; my = fvalues [1]-accdefaulty; log. E ("test", "vlaue [0]:" + mx + "value [1]:" + my); If (orientation = 0) {I F (My <-2.5 & math. Abs (MX) <0.5 & current_dir! = 1) {current_dir = 1; // The Front log. E ("atest", orientation + "" + current_dir);} else if (My> 2.5 & math. ABS (MX) <0.5 & current_dir! = 2) {current_dir = 2; // post log. E ("atest", orientation + "" + current_dir);} else if (math. ABS (MX) <0.5 & math. ABS (my) <0.5 & current_dir! = 3) {current_dir = 3; // stop log. E ("atest", orientation + "" + current_dir);} else if (MX> 1.5 & my <= 0.0 & current_dir! = 4) {current_dir = 4; // turn left/turn left to log. E ("atest", orientation + "" + current_dir);} else if (MX <-1.5 & my <= 0.0 & current_dir! = 5) {current_dir = 5; // turn right/log in front of right. E ("atest", orientation + "" + current_dir);} else if (MX> 1.5 & my> 1.5 & current_dir! = 6) {current_dir = 6; // turn left back to log. E ("atest", orientation + "" + current_dir);} else if (MX <-1.5 & my> 1.5 & current_dir! = 7) {current_dir = 7; // right back to log. E ("atest", orientation + "" + current_dir) ;}} else if (orientation = 1) {If (MX <-2.5 & math. ABS (my) <0.5 & current_dir! = 1) {current_dir = 1; // The Front log. E ("atest", orientation + "" + current_dir) ;}else if (MX> 2.5 & math. ABS (my) <0.5 & current_dir! = 2) {current_dir = 2; // post log. E ("atest", orientation + "" + current_dir);} else if (math. ABS (MX) <0.5 & math. ABS (my) <0.5 & current_dir! = 3) {current_dir = 3; // stop log. E ("atest", orientation + "" + current_dir);} else if (MX <= 0.0 & my <-1.5 & current_dir! = 4) {current_dir = 4; // turn left/turn left to log. E ("atest", orientation + "" + current_dir);} else if (MX <= 0.0 & my> 1.5 & current_dir! = 5) {current_dir = 5; // turn right/log in front of right. E ("atest", orientation + "" + current_dir);} else if (MX> 1.5 & my <-1.5 & current_dir! = 6) {current_dir = 6; // turn left back to log. E ("atest", orientation + "" + current_dir);} else if (MX> 1.5 & my> 1.5 & current_dir! = 7) {current_dir = 7; // right back to log. E ("atest", orientation + "" + current_dir) ;}} else if (sensor_type = sensor. type_orientation) {If (orientation = 0) {fvalues = event. values; log. E ("test", "vlaue [0]:" + fvalues [0] + "value [1]:" + fvalues [1] + "value [2]: "+ fvalues [2]); If (fvalues [1]> Angle & (math. ABS (fvalues [2]) <5) & current_dir! = 1) {current_dir = 1; // The Front log. E ("atest", orientation + "" + current_dir);} else if (fvalues [1] <-angle & (math. ABS (fvalues [2]) <5) & current_dir! = 2) {current_dir = 2; // post log. E ("atest", orientation + "" + current_dir);} else if (math. ABS (fvalues [1]) <5 & math. ABS (fvalues [2]) <5 & current_dir! = 3) {current_dir = 3; // stop log. E ("atest", orientation + "" + current_dir);} else if (fvalues [1]> 0 & fvalues [2]> 15 & current_dir! = 4) {current_dir = 4; // turn left/turn left to log. E ("atest", orientation + "" + current_dir);} else if (fvalues [1]> 0 & fvalues [2] <-15 & current_dir! = 5) {current_dir = 5; // turn right/log in front of right. E ("atest", orientation + "" + current_dir);} else if (fvalues [1] <-15 & fvalues [2]> 15 & current_dir! = 6) {current_dir = 6; // turn left back to log. E ("atest", orientation + "" + current_dir);} else if (fvalues [1] <-15 & fvalues [2] <-15 & current_dir! = 7) {current_dir = 7; // right back to log. E ("atest", orientation + "" + current_dir) ;}} else if (orientation = 1) {fvalues = event. values; log. E ("test_1", "vlaue [0]:" + fvalues [0] + "value [1]:" + fvalues [1] + "value [2]: "+ fvalues [2]); If (fvalues [2] <-angle & (math. ABS (fvalues [1]) <5) & current_dir! = 1) {current_dir = 1; // The Front log. E ("atest", orientation + "" + current_dir);} else if (fvalues [2]> Angle & (math. ABS (fvalues [1]) <5) & current_dir! = 2) {current_dir = 2; // post log. E ("atest", orientation + "" + current_dir);} else if (math. ABS (fvalues [1]) <5 & math. ABS (fvalues [2]) <5 & current_dir! = 3) {current_dir = 3; // stop log. E ("atest", orientation + "" + current_dir);} else if (fvalues [1]> 15 & fvalues [2] <= 5 & current_dir! = 4) {current_dir = 4; // turn left/turn left to log. E ("atest", orientation + "" + current_dir);} else if (fvalues [1] <-15 & fvalues [2] <= 5 & current_dir! = 5) {current_dir = 5; // turn right/log in front of right. E ("atest", orientation + "" + current_dir);} else if (fvalues [1]> 15 & fvalues [2]> 15 & current_dir! = 6) {current_dir = 6; // turn left back to log. E ("atest", orientation + "" + current_dir);} else if (fvalues [1] <-15 & fvalues [2]> 15 & current_dir! = 7) {current_dir = 7; // right back to log. E ("atest", orientation + "" + current_dir) ;}}@ overridepublic void onaccuracychanged (sensor, int accuracy) {}@ overrideprotected void ondestroy () {super. ondestroy ();} public int current_dir = 0 ;}

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.