Skip to content

Instantly share code, notes, and snippets.

Created March 1, 2018 10:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Main Activity for the Barometer App on the SSaurel's Channel
package com.ssaurel.barometer;
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 AppCompatActivity {
private TextView txt;
private SensorManager sensorManager;
private Sensor pressureSensor;
private SensorEventListener sensorEventListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent sensorEvent) {
float[] values = sensorEvent.values;
txt.setText(String.format("%.3f mbar", values[0]));
public void onAccuracyChanged(Sensor sensor, int i) {
protected void onCreate(Bundle savedInstanceState) {
txt = findViewById(;
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
pressureSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
protected void onResume() {
sensorManager.registerListener(sensorEventListener, pressureSensor, SensorManager.SENSOR_DELAY_UI);
protected void onPause() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment