Search code examples
androidaccelerometer

Android accelerometer returns 0.0


I have written code to get the coordinates returned by the accelerometer and print them on TextViews on click of a button. However, I get only 0.0 as the value on all labels. I have also added the setText part in the onSensorChange() method just to check if the values change every time the mobile is somewhat shaken. I have added the necessary permission in Manifest too. Please help me to make this work.

Code:

package com.example.lol;

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.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener{

    private SensorManager senSensorManager;
    private Sensor senAccelerometer;
    private long lastUpdate = 0;
    private float last_x, last_y, last_z;
    public float dummy_x, dummy_y,dummy_z;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(onClickListener);

        senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        senSensorManager.unregisterListener(this);
    }
    protected void onResume() {
        super.onResume();
        senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onSensorChange(SensorEvent sensorEvent) {
        Sensor mySensor = sensorEvent.sensor;

        if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];
            dummy_x=x;
            dummy_y=y;
            dummy_z=z;
            TextView textView1 = (TextView)findViewById(R.id.textView1);
            TextView textView2 = (TextView)findViewById(R.id.textView2);
            TextView textView3 = (TextView)findViewById(R.id.textView3);
            textView1.setText(Float.toString(x));
            textView2.setText(Float.toString(y));
            textView3.setText(Float.toString(z));
        }
    }

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(final View v) {
            switch(v.getId()){
                case R.id.button1:
                    TextView textView1 = (TextView)findViewById(R.id.textView1);
                    TextView textView2 = (TextView)findViewById(R.id.textView2);
                    TextView textView3 = (TextView)findViewById(R.id.textView3);
                    textView1.setText(Float.toString(dummy_x));
                    textView2.setText(Float.toString(dummy_y));
                    textView3.setText(Float.toString(dummy_z));
                break;
            }
        }
    };

    @Override
    public void onSensorChanged(SensorEvent event) {

    }

}

Solution

  • You have 2 OnSensorChange functions. The first one with all your TextView code isn't overriding anything. The one at the bottom (empty) is the one where the values will come.