Android method for obtaining gravitational induction acceleration based on sensor sensors _android

Source: Internet
Author: User

This article describes the Android approach to obtaining gravitational induction acceleration based on sensor sensors. Share to everyone for your reference, specific as follows:

FETC Project Guidance teacher put forward new requirements, want to show in the game map user users in the direction of the period of movement, and then using GPS is obviously very unreliable, so think of the android powerful sensors ...

Many mobile devices have sensors built into them, and Android abstracts these sensors through sensor and Sensormanager classes that allow you to use the Android device's sensors

One introduction to Sensor class

The SDK has only one sentence to introduce "Class representing a sensor." Use getsensorlist (int) to get the list of available sensors. "Represents a class of sensors, You can use the Getsensorlist method (this method belongs to the next sensormanager) to get all the available sensors, which returns a list<sensor>

The following list shows all the services provided by sensor

Constants
int Type_accelerometer A constant describing an accelerometer sensor TYPE.
Three-axis accelerometer returns three axes of acceleration unit M/S2
int Type_all A constant describing all sensor types.
Used to list all sensors
int type_gravity A constant describing a gravity sensor TYPE.
Gravity sensor
int Type_gyroscope A constant describing a gyroscope sensor TYPE
Gyro can be judged by the direction of return three axes angle
int Type_light A constant describing an LIGHT sensor TYPE.
Ray Sensor Unit LUX Lux
int Type_linear_acceleration A constant describing a LINEAR acceleration sensor TYPE.
Linear acceleration
int Type_magnetic_field A constant describing a magnetic FIELD sensor TYPE.
Magnetic field Induction Returns a numerical value of three axes. Micro Tesla
int Type_orientation This constant is deprecated. Use Sensormanager.getorientation () instead.
The direction sensor is obsolete and can be obtained using the method
int Type_pressure A constant describing a pressure sensor TYPE
Pressure sensor unit thousand Pascal
int type_proximity A constant describing an proximity sensor TYPE.
Distance sensor
int Type_rotation_vector A constant describing a rotation VECTOR sensor TYPE.
Flip Sensor
int Type_temperature A constant describing a temperature sensor TYPE
Temperature Sensor Unit Celsius

The method contained in this class is used to get some of the properties of the selected sensor, sensor class does not need new but is obtained by Sensormanager method

Two introduces Sensormanager class

SDK Explanation: "Sensormanager lets you access the device ' s sensors." Get a instance of this class by calling Context.getsystemservice () with the argument Sensor_service.
Always Make sure to disable sensors your don ' t need, especially when your the activity is paused. Failing to did can drain the battery in just a few hours. Note that the system won't disable sensors automatically when the screens turns off. ”
Sensormanager allows you to access the device's sensors. A sensor instance can be obtained by invoking the Context.getsystemservice method by passing in the parameter Sensor_service parameter. Always remember to turn off the sensor when you don't need it, especially if the activity is tentative. Ignoring this can lead to a few hours of running out of batteries, and note that the system does not automatically turn off the sensor when the screen is closed.

Three commonly used sensors

(1) Acceleration sensor

You can get three floating-point types from this sensor

X-axis
Y-axis
Z-axis

See an illustration in "Android Advanced Programming 2" to analyze the secondary data

X Y z corresponds respectively to values[0] to [2]
X represents the acceleration of the left and right movements
Y indicates the acceleration of movement before and after

Z Represents the vertical acceleration (the test found that the cell phone to the level of the desktop after the stable xy 0 Z value of 9.4 is equal to the gravitational acceleration, in turn, can do a simple algorithm to achieve the gravity dynamometer, time will give you an example)

Let's look at a basic to get the accelerated demo, I hope you pay attention to the comments in the code

* * * @author Octobershiner * SE.
HIT * An example of a demo android Accelerometer * * * * * * Package uni.sensor;
Import Java.util.Iterator;
Import java.util.List;
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; The public class Sensordemoactivity extends activity {/** called the ' when the ' is the ' The activity ' is the ' the './/Set Log tag created
  Te static final String TAG = "sensor";
  Private Sensormanager SM;
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Create a Sensormanager to get the system's sensor services SM = (Sensormanager) getsystemservice (Context.sensor_service);
    Select the acceleration sensor int sensortype = Sensor.type_accelerometer; * * The most commonly used method of registering event * Parameters 1:sensoreventlistener Listener * Parameters 2:sensor a service may have moreA sensor implementation that calls Getdefaultsensor to get the default sensor * Parameter 3: Refresh frequency for changes in mode-selectable data * */Sm.registerlistener (myaccelerometerlist
  Ener,sm.getdefaultsensor (SensorType), sensormanager.sensor_delay_normal); * * * * Sensoreventlistener interface implementation, need to implement two methods * Method 1 onsensorchanged When the data changes are triggered call * Method 2 onaccuracychanged When the accuracy of the data is obtained Changes are invoked when the data is suddenly unavailable * */FINAL Sensoreventlistener Myaccelerometerlistener = new Sensoreventlistener () {//Replication Onse Nsorchanged method public void onsensorchanged (Sensorevent sensorevent) {if (sensorEvent.sensor.getType () = = Sensor.ty
        Pe_accelerometer) {log.i (TAG, "onsensorchanged");
        The meaning of three values has been explained in the diagram float x_lateral = sensorevent.values[0];
        float y_longitudinal = sensorevent.values[1];
        float z_vertical = sensorevent.values[2];
        LOG.I (TAG, "\ n heading" +x_lateral);
        LOG.I (TAG, "\ n pitch" +y_longitudinal);
      LOG.I (TAG, "\ n Roll" +z_vertical); }///replication onaccuracychanged method public void OnaccuracychAnged (Sensor Sensor, int accuracy) {LOG.I (TAG, "onaccuracychanged");
  }
  }; public void OnPause () {* * * is a critical part: Note that the documentation mentions that the sensor will continue to work even when the activity is not visible, and that the test can be found to be very high without a normal refresh rate, so be sure to
     Turn off triggers in the OnPause method, otherwise it is not responsible for consuming a lot of user power.
    * * */Sm.unregisterlistener (Myaccelerometerlistener);
  Super.onpause ();

 }
}

The test will find that the refresh is very fast, which leads to a problem, if you really want to render in the UI, it will continue to draw the interface, very resource-intensive, so "Android advanced Programming," a solution is to introduce a new thread to refresh the interface, tomorrow there is time, as far as possible to give the example to everyone.

I hope this article will help you with the Android program.

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.