Android accelerometer class


I tried to create seperate accelerometer class that holds values of accelerometer and I can acces them from any other class whenever I want, but it doesn't seem to work.

Here is my Accelerometer class:

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;

public class Accelero implements SensorEventListener {

    private float xAxis;
    private float yAxis;
    private float zAxis;

    public float getX(){
        return this.xAxis;

    public float getY(){
        return this.yAxis;

    public float getZ(){
        return this.zAxis;

    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub


    public void onSensorChanged(SensorEvent event) {
            xAxis = event.values[0];
            yAxis = event.values[1];
            zAxis = event.values[2];


so as you see I am trying to get values through get*(); method that I created. But it seems always to return 0, 0, 0 . Any idea what is wrong with my code?

Here is the Activity that this is used in:

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText output;
    private static int DIS = 1;
    Accelero acc;

    public void onCreate(Bundle savedInstanceState) {

        output =(EditText) findViewById(;
        handler.sendEmptyMessageDelayed(DIS, 1);
        acc = new Accelero(this);


    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            if (msg.what == DIS){
                handler.sendEmptyMessageDelayed(DIS, 1);

    public void refresh(){

I also have this premission in manifest:

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

You have to register the sensor that you are using, in this case the accelerometer. I'm guessing you want everything related to the accelerometer in the "Accelero" class.

To register the listener from that class, you'll have to pass a reference to your activity. So your constructor should look something like this:

SensorManager manager;
Sensor accelerometer;
Activity foo;

public Accelero(Activity foo) { = foo;
    manager = (SensorManager);
    accelerometer = manager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    manager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);

and when you create a Accelero object, you simply pass this as the parameter.

I hope this helps.