Android high score advanced strategy (1) sensor

Source: Internet
Author: User

Question: many of my friends have asked me to write an android tutorial. I think there are already many great tutorials on the Internet, especially the videos of Lao Luo and Mars are great. I am just a little rookie, he de can give me a tutorial. Many times I have told you not to trust me in private, but many people trust me in private. Here I think many problems cannot be solved by myself, you can post a post on the Forum to solve the problem together. However, since everyone trusts me so much, I will write some project experiences and share them with you. I will not talk about the simple foundation. Let's start with the Project. The article may be uploaded in a timely manner, I have also encountered many problems. After reading these questions, I will give some guidance on related demos. Due to copyright issues and problems related to me from poor mountainous areas, some good demos may need to be paid for download, intellectual property rights. By the way, I donated my articles from poor mountainous areas, which may not be very professional, because I also shared my articles with you. By the way, I can better learn about android, I don't want to talk about it any more. The advanced strategy of the android high score section starts from now on.

When did we get to know that there are sensors, which are actually the first generation of the iphone? as more and more applications start to connect with hardware, the user experience will be improved, and the sensor will start to turn from unreliable to the trend of the times, what is the sensor? direction, gravity, acceleration, light, magnetic field, closeness, temperature, etc. In android, all sensors depend on the android. hardware. SensorEventListener interface. Now let's take a look at how the sdk is written:

public interface SensorEventListener     {        public void onSensorChanged(SensorEvent event);        public void onAccuracyChanged(Sensor sensor, int accuracy);        }  

This interface is actually simple. There are only two methods. Literally, we can also know that when the sensor changes, the onSensorChanged method will be called, so the focus is to understand this method, then let's take a look at the SensorEvent parameter passed in and continue to view the SensorEvent sdk. It is not difficult to find that SensorEvent has four attributes:

Public int accuracy, public Sensor, public long timestamp, public final float [] values

The full text is mostly used to describe the values attribute, because values is used to describe the value of the sensor. I have found relevant information about this values on the Internet, although I do not remember the ID of the great god, thank you for sharing this.

The three values of the values variable in the direction sensor indicate the degree. Their meanings are as follows:

Values [0]: This value indicates the orientation, that is, the angle from which the mobile phone rotates around the Z axis. 0 indicates North (North); 90 indicates East (East); 180 indicates South (South); 270 indicates West (West ). If the value of values [0] is exactly the four values, and the mobile phone is placed horizontally, it indicates that the front of the mobile phone is the four directions. This feature can be used to implement the electronic compass.
Values [1]: This value indicates the inclination or the mobile phone climbing. This value changes when the phone is tilted around the X axis. The value range of values [1] is-180 ≤ values [1] ≤ 180. Assume that the screen of the mobile phone is placed horizontally on the table. If the table is completely horizontal, the value of values [1] should be 0 (since few tables are absolutely horizontal, this value may not be 0, but is generally a value between-5 and 5 ). At this time, the phone is raised from the top of the phone until it is rotated 180 degrees along the X axis (the screen is horizontally placed on the desktop ). During this rotation process, values [1] will change between 0 and-180. That is to say, when the value of values [1] is lifted from the top of the mobile phone, it will gradually decrease, until it is equal to-180. If the mobile phone is raised from the bottom until it is rotated 180 degrees along the X axis, then values [1] will change between 0 and 180. That is, the value of values [1] will gradually increase until it is equal to 180. You can use values [1] and the values [2] to measure the inclination of objects such as tables.
Values [2]: indicates the scroll angle of the mobile phone along the Y axis. The value range is-90 ≤ values [2] ≤ 90. Assume that the screen of the mobile phone is placed horizontally on the desktop. If the desktop is flat, the value of values [2] should be 0. When the left side of the phone is gradually raised, the value of values [2] gradually decreases until the phone is placed perpendicular to the desktop. Then, the value of values [2] is-90. When the right side of the phone is gradually raised, the value of values [2] increases gradually until the phone is placed perpendicular to the desktop, then the value of values [2] is 90. Scroll to the right or left when the vertical position is specified. The value of values [2] will continue to change between-90 and 90.


In the acceleration sensor, the three element values of the values variable represent the acceleration values of the X, Y, and Z axes respectively. For example, if a mobile phone is horizontally placed on the desktop, it moves from the left to the right. values [0] is a negative value, and values [0] is a positive value.


In a gravity sensor, the gravity sensor and the acceleration sensor use the same coordinate system. The three elements in the values array indicate the gravity of the X, Y, and Z axes respectively.


In a light sensor, the alues array has only the first element (values [0]) that makes sense. Indicates the intensity of light. The maximum value is 120000.0f.


In a gyroscope Sensor, the constant type of the gyroscope Sensor is Sensor. TYPE_GYROSCOPE. The meanings of the three elements in the values array are as follows: values [0]: the angular velocity of the X axis rotation. Values [1]: returns the angular velocity of the Y axis rotation. Values [2]: returns the angular velocity of the Z axis rotation. When the mobile phone rotates counterclockwise, the angular velocity is positive. When the mobile phone rotates clockwise, the angular velocity is negative.


After introducing the basic sensor types, we started to write our first sensor. The specific ideas are as follows:

1. Excuses for instantiating Sensors

2. Obtain the Sensor Management Interface

3. register the Sensor


Public class MainActivity extends Activity implements SensorEventListener {private TextView listener; private TextView tvMagentic; private TextView tvLight; private TextView tvOrientation; private TextView tvSensors; @ Override public void onCreate (Bundle listener) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the SensorManager object SensorManager sensorManager = (SensorManager) getSystemService (SENSOR_SERVICE); // register the acceleration sensor sensorManager. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER), SensorManager. SENSOR_DELAY_FASTEST); // register the Magnetic Field Sensor sensorManager. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_MAGNETIC_FIELD), SensorManager. SENSOR_DELAY_FASTEST); // register the light sensor sensorManager. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_LIGHT), SensorManager. SENSOR_DELAY_FASTEST); // registers the sensor sensorManager. registerListener (this, sensorManager. getdefasensensor (Sensor. TYPE_ORIENTATION), SensorManager. SENSOR_DELAY_FASTEST); tvAccelerometer = (TextView) findViewById (R. id. tvAccelerometer); tvMagentic = (TextView) findViewById (R. id. tvMagentic); tvLight = (TextView) findViewById (R. id. tvLight); tvOrientation = (TextView) findViewById (R. id. tvOrientation); tvSensors = (TextView) findViewById (R. id. tvSensors); // obtain the List of all sensors supported by the current mobile phone <Sensor> sensors = sensorManager. getSensorList (Sensor. TYPE_ALL); for (Sensor sensor: sensors) {// output the name of the current Sensor, tvSensors. append (sensor. getName () + "\ n") ;}@ Override public void onSensorChanged (SensorEvent event) {// use the getType method to obtain the sensor type switch (event. sensor. getType () {case Sensor. TYPE_ACCELEROMETER: // process the data returned by the accelerometer String accelerometer = "Acceleration \ n" + "X:" + event. values [0] + "\ n" + "Y:" + event. values [1] + "\ n" + "Z:" + event. values [2] + "\ n"; tvAccelerometer. setText (accelerometer); break; case Sensor. TYPE_LIGHT: // process the data transmitted by the light sensor tvLight. setText ("brightness:" + event. values [0]); break; case Sensor. TYPE_MAGNETIC_FIELD: // process the data returned by the magnetic field sensor String magentic = "magnetic field \ n" + "X:" + event. values [0] + "\ n" + "Y:" + event. values [1] + "\ n" + "Z:" + event. values [2] + "\ n"; tvMagentic. setText (magentic); break; case Sensor. TYPE_ORIENTATION: // The data returned by the processing direction sensor String orientation = "\ n" + "X:" + event. values [0] + "\ n" + "Y:" + event. values [1] + "\ n" + "Z:" + event. values [2] + "\ n"; tvOrientation. setText (orientation); break ;}@ Override public void onAccuracyChanged (Sensor sensor, int accuracy ){}}

Because this example is too simple and the code is also the teaching code found on the internet, I will not write the demo. Today I will introduce the principle of sensors to you. Next time we will begin to use sensors in an advanced manner, I will introduce a pedometer app that I used previously. I hope you will like it. Please try again later.

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.