@Override protected void onResume() { super.onResume(); final TextView sensorValues = (TextView) findViewById(R.id.sensor_values); sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY); gravityListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { double length = Math.sqrt( event.values[0] * event.values[0] + event.values[1] * event.values[1] + event.values[2] * event.values[2]); double alfa = Math.asin(event.values[0] / length) * 180 / Math.PI; double beta = Math.asin(event.values[1] / length) * 180 / Math.PI; sensorValues.setText(alfa + "\n" + beta); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) {} }; sensorManager.registerListener(gravityListener, sensor, SensorManager.SENSOR_DELAY_GAME); }
@Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(gravityListener); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }