Skip to content

Instantly share code, notes, and snippets.

@mlashcorp
Created January 5, 2013 16:24
Show Gist options
  • Save mlashcorp/4462330 to your computer and use it in GitHub Desktop.
Save mlashcorp/4462330 to your computer and use it in GitHub Desktop.
Android accelerometer polling, just the creation and update portions. Updated the class to implement SensorEventListener, since SensorListener is deprecated
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
accell = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
setContentView(R.layout.activity_main);
xacc = (TextView) findViewById(R.id.xvalue);
yacc = (TextView) findViewById(R.id.yvalue);
zacc = (TextView) findViewById(R.id.zvalue);
}
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
xacc.setText("Accel X: " + event.values[0]);
yacc.setText("Accel Y: " + event.values[1]);
zacc.setText("Accel Z: " + event.values[2]);
}
if(logging)
data.add(event.values);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment