Skip to content

Instantly share code, notes, and snippets.

@ishitcno1
Last active December 27, 2015 01:29
Show Gist options
  • Save ishitcno1/7245433 to your computer and use it in GitHub Desktop.
Save ishitcno1/7245433 to your computer and use it in GitHub Desktop.
Show accelerometer values of android device to help define motions.
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mSensor;
private TextView tvSensorData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
tvSensorData = (TextView)findViewById(R.id.tv_sensor_data);
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
float z = sensorEvent.values[2];
String s = "X: " + x + "\nY: " + y + "\nZ: " + z;
tvSensorData.setText(s);
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment