Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the Android Accelerometer?

I'm trying to build an app for reading the values from the accelerometer on my phone, which supports Android 2.1 only.

How do I read from the accelerometer using 2.1-compatible code?

like image 872
user642866 Avatar asked Mar 03 '11 11:03

user642866


People also ask

How do we use accelerometer?

Accelerometers can be used to measure vibration on cars, machines, buildings, process control systems and safety installations. They can also be used to measure seismic activity, inclination, machine vibration, dynamic distance and speed with or without the influence of gravity.

How do I access the accelerometer on my phone?

To use the accelerometer (or any sensor in general) your class should implement the SensorEventListener interface, or you could do anonymous inner classes for them. To access the accelerometer you will need to get the SystemManager from the system and get a sensors list from that. sensors = myManager.

How do I test my Android accelerometer?

The accelerometer detects the orientation of your phone and measures its linear acceleration of movement. To check if it's working properly, tap on either "Image Test" to check for landscape-to-portrait transitions, or "Graph" to see how well the sensor detects movement by shaking your device up and down.

What is the use of accelerometer in mobile phones?

Accelerometers in mobile phones are used to detect the orientation of the phone. The gyroscope, or gyro for short, adds an additional dimension to the information supplied by the accelerometer by tracking rotation or twist.


1 Answers

Start with this:

public class yourActivity extends Activity implements SensorEventListener{  private SensorManager sensorManager;  double ax,ay,az;   // these are the acceleration in x,y and z axis  @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);         sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);    }    @Override    public void onAccuracyChanged(Sensor arg0, int arg1) {    }     @Override    public void onSensorChanged(SensorEvent event) {         if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){             ax=event.values[0];                     ay=event.values[1];                     az=event.values[2];             }    } } 
like image 115
ArcDare Avatar answered Sep 20 '22 14:09

ArcDare