Android Accessing Accelerometer: Returns always 0 as Value

Posted by Rotesmofa on Stack Overflow See other posts from Stack Overflow or by Rotesmofa
Published on 2010-12-30T19:38:12Z Indexed on 2010/12/30 19:54 UTC
Read the original article Hit count: 329

Filed under:
|
|
|

Hello there,

i would like to use the accelerometer in an Android Handset for my Application. The data from the sensor will be saved together with a GPS Point, so the Value is only needed when the GPS Point is updated.

If i use the attached Code the values is always zero. API Level 8 Permissions: Internet, Fine Location Testing Device: Galaxy S(i9000), Nexus One

Any Suggestions? I am stuck at this point.

Best regards from Germany, Pascal

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class AccelerometerService extends Activity{
    AccelerometerData accelerometerData;
    private SensorManager mSensorManager;
    private float x,y,z;
    private class AccelerometerData implements SensorEventListener{ 

        public void onSensorChanged(SensorEvent event) {
            x = event.values[0];
            y = event.values[1];
            z = event.values[2];
        }
        public void onAccuracyChanged(Sensor sensor, int accuracy) {}
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mSensorManager.registerListener(accelerometerData,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);
    }
    @Override
    protected void onResume() {
        super.onResume();  
    }
    @Override
    protected void onStop() {
        mSensorManager.unregisterListener(accelerometerData);
        super.onStop();
    }
    public String getSensorString()
    {
        return ("X: " + x+"m/s, Y: "+ y +"m/s, Z: "+ z +"m/s" );
    }
}

© Stack Overflow or respective owner

Related posts about android

Related posts about null