Search code examples
javaandroidclassaccelerometer

Push Accelerometer values to another class


I have looked at many other threads that show how to use accelerometer values, and even go across multiple classes, however none of the methods that I have found work and no matter what I do, I cannot get the values to display in a class other than in the one they are defined in. I know I have a working accelerometer and that my AccelerometerReader class is getting the values correctly, because I have tested them out by printing them in that class. I have some code below, but I can't find out why it isn't working, although I believe that only the starting values are being passed into another class, and that they are not updating. Any help would be much appreciated!

AccelerometerReader Class

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 AccelerometerReader extends Activity implements SensorEventListener {

private SensorManager sensorManager;
private float ax;   // these are the acceleration in x, y and z axes
private float ay;
private float az;

public AccelerometerReader(float x, float y, float z) {
    this.ax = x;
    this.ay = y;
    this.az = z;
}

@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_FASTEST);
   }

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

 @Override
 public void onSensorChanged(SensorEvent event) {
     if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
           return;

     if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
           setValueX(event.values[0]);
           setValueY(event.values[1]);
           setValueZ(event.values[2]);

     }

 }


 public void setValueX(float x) {
     ax=x;
 }

 public float getValueX() {
    return ax;
 }

 public void setValueY(float y) {
     ay=y;
 }

 public float getValueY() {
     return ay;
 }

 public void setValueZ(float z) {
     az=z;
 }

 public float getValueZ() {
     return az;
 }
}

Accelerometer Class

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Accelerometer extends Activity {

private float ax, ay, az;
TextView tvX, tvY, tvZ;
AccelerometerReader acc = new AccelerometerReader(ax, ay, az);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tvX = (TextView) findViewById(R.id.tvX);
    tvY = (TextView) findViewById(R.id.tvY);
    tvZ = (TextView) findViewById(R.id.tvZ);

    tvX.setText("" + ax);
    tvY.setText("" + ay);
    tvZ.setText("" + az);
}
}

Solution

  • You have a ton of problems in the code:

    1. You have one Activity that performs a function (i.e. Get the "ACCELEROMETER" values), however you want to perform this function while the user is looking at a different activity. This is generally bad and won't work. You will instead need to create a Service.

    2. You are passing values from one class to another, when you pass those values they are just copies. So anything that is done to those values from the other class does not affect the original class.

      //What ever happens to ax, ay, and az will never be known to your class!
      AccelerometerReader acc = new AccelerometerReader(ax, ay, az);
      
    3. You do this once:

      tvX.setText("" + ax);
      tvY.setText("" + ay);
      tvZ.setText("" + az);
      

      even if ax, ay, and az changed the current code will only display them once. You have to recall that code every time the values change for them to be displayed.

    Well, good luck!