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.app.Activity;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output =(EditText) findViewById(R.id.editText1);
handler.sendEmptyMessageDelayed(DIS, 1);
acc = new Accelero(this);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == DIS){
refresh();
handler.sendEmptyMessageDelayed(DIS, 1);
}
}
};
public void refresh(){
output.setText("X:"+acc.getX()+"\nY:"+acc.getY()+"\nZ:"+acc.getZ());
}
}
I also have this premission in manifest:
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />