Gravity sphere-Application of gravity Sensor

Source: Internet
Author: User

The first Google summer College Students' blog sharing competition-2010 andriod

I saw a gravity sensor app on HTC's Windows Mobile phone on the Internet.Program"The ball of the pain", address: http://v.youku.com/v_show/id_XMTkxOTc0MDg0.html recently I am learning Android system development, a little eager to try the feeling, want to do. As a result, I started to prepare for the work, so I did not mention the materials.

The first step is to use the sensor in Android.

Android has many APIs that can directly call sensors in mobile phones. These sensors can be used to obtain the speed, space location, orientation, light sensing, and magnetic field of mobile phones in real environments, the pressure gauge, temperature, and other information can be used to provide some interesting functions.

1. First, you need to get a sensor on your mobile phone,CodeRelatively simple. Through this line of code, we can get a controller of the hardware device.

  Sensormanager mmanager=(Sensormanager) getsystemservice (sensor_service );

2. Use the hardware controller obtained in the previous step to obtain the desired sensor, such as a gravity sensor. Note that some mobile phones may not support the sensors we need, so msensor is null. Therefore, to ensure security, you should determine whether the sensor is null.

 
  Sensor msensor=Sensormgr. getdefasensensor (sensor. type_accelerometer );

There are many other types of parameters. Description of each type in the official document:

Int

Type_accelerometer

A constant describing an Accelerometer Sensor type.

Int

Type_all

A constant describing all sensor types.

Int

Type_gyroscope

A constant describing a gyroscope sensor type

Int

Type_light

A constant describing an light sensor type.

Int

Type_magnetic_field

A constant describing a magnetic field sensor type.

Int

Type_orientation

A constant describing an orientation sensor type.

Int

Type_pressure

A constant describing a pressure sensor type.

Int

Type_proximity

A constant describing an proximity sensor type.

Int

Type_temperature

A constant describing a temperature sensor type.

As only the gravity sensor is needed this time, that is, the "acceleration" type_accelerometer. Other sensors will not be used this time and will be used in the future.

3. Through the data of the gravity sensor, we can know the deflection of the mobile phone, and then we can control the movement of the ball. Here, we will explain the X, Y, and Z coordinate systems on the opponent's machine, and explain them with images from some online users.

Take the lower-left corner of the screen as the origin, the y-axis is the positive direction on the screen, the right side is the positive direction of the X axis, the front side of the mobile phone screen is the positive direction of the Z axis, the data returned by the sensor is float type, and the positive direction returns integrity, negative direction returns negative value, range from-10 to 10, so according to the data and trigonometric function, you can calculate the mobile phone motion status.

4. To get the sensor data, you need to use a listener. When the mobile phone status changes, you will get the latest data. This requires sensoreventlistener. You need to override public void onsensorchanged (sensorevent E) and Public void onaccuracychanged (sensor S, int accuracy ), the former is triggered when the sensor data changes, and the latter is triggered when the sensor sensitivity changes.

 

  1   Sensoreventlistener listener  =     New  Sensoreventlistener (){
2 Public Void Onsensorchanged (sensorevent e ){
3 X = E. Values [sensormanager. data_x];
4 Y = E. Values [sensormanager. data_y];
5 Z = E. Values [sensormanager. data_z];
6 // Do something with x, y, z
7   }
8  

5. The code in the previous step can easily show the simplicity and convenience of the listener. In the next step, we need to register the listener, and there is only one line of code for the registration method.

 
  Sensormgr. registerlistener (listener, msensor, sensormanager. sensor_delay_game );

The first two parameters have been defined above. The third parameter is the sensitivity of the sensor. The official document describes four sensitivity Atmospheres:

Sensor_delay_fastestMost sensitive

Sensor_delay_gameThis is used in the game, and the sensitivity is relatively high.
Sensor_delay_normalNormal

Sensor_delay_uiThe most insensitive is generally horizontal and vertical

Through the above steps, we can easily obtain sensor data and process the data to perform different events. Of course, this requires a lot of experiments and imagination from developers. For the moment, here is the gravity sensor.

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.