Displaying Accelerometer Values in Android

27,095

Solution 1

Have you checked out the Accelerometer example in the android samples? You could always try something like this (which I got from here):

 public class SensorActivity extends Activity implements SensorEventListener {
     private final SensorManager mSensorManager;
     private final Sensor mAccelerometer;

     public SensorActivity() {
         mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
         mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
     }

     protected void onResume() {
         super.onResume();
         mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
     }

     protected void onPause() {
         super.onPause();
         mSensorManager.unregisterListener(this);
     }

     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }

     public void onSensorChanged(SensorEvent event) {
       //Right in here is where you put code to read the current sensor values and 
       //update any views you might have that are displaying the sensor information
       //You'd get accelerometer values like this:
       if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
            return;
        float mSensorX, mSensorY;
        switch (mDisplay.getRotation()) {
            case Surface.ROTATION_0:
                mSensorX = event.values[0];
                mSensorY = event.values[1];
                break;
            case Surface.ROTATION_90:
                mSensorX = -event.values[1];
                mSensorY = event.values[0];
                break;
            case Surface.ROTATION_180:
                mSensorX = -event.values[0];
                mSensorY = -event.values[1];
                break;
            case Surface.ROTATION_270:
                mSensorX = event.values[1];
                mSensorY = -event.values[0];
         }
     }
 }

Solution 2

Here is another example with gravitation, this will return the vector of gravity. Note that you can change the sensor type and the speed of sampling, more details here

SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

sensorManager.registerListener(new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        double total = Math.sqrt(x * x + y * y + z * z);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

}, sensor, SensorManager.SENSOR_DELAY_FASTEST);
Share:
27,095
Slicekick
Author by

Slicekick

Rails.

Updated on January 29, 2020

Comments

  • Slicekick
    Slicekick over 4 years

    I am looking for some source code or example code detailing how to display the values of the accelerometer in Android. Preferably, the values would show acceleration in the x direction, y direction, and z direction.

    I am an Android noob so any help is much appreciated.

  • Slicekick
    Slicekick over 12 years
    Yes, but I don't know how to use that information to display basic accelerometer values.
  • Kurtis Nusbaum
    Kurtis Nusbaum over 12 years
    Updated my answer. Check it out.
  • Slicekick
    Slicekick over 12 years
    I see, but in the commented section, what exactly do I need to put in to read the values?
  • Kurtis Nusbaum
    Kurtis Nusbaum over 12 years
    I lifted some code directly from the android accelerometer example and put it in my answer.
  • KK_07k11A0585
    KK_07k11A0585 over 6 years
    @KurtisNusbaum From where you get the mDisplay.getRotation() ??
  • Kurtis Nusbaum
    Kurtis Nusbaum over 6 years
    @KK_07k11A0585 please checkout the example I linked to
  • James Poulose
    James Poulose about 3 years
    For those who are looking for getRotation, use this Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(‌​);