I am working on a basic barometer app and I have run into a slight snag. I want to take the data that is being received and have it run through a text view that I have setup just for the pressure variable. Here is my code to this point:
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.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Barometer extends Activity {
TextView baroText, timeText;
Button refresh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.barometer_layout);
uiVars();
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
sensorManager.registerListener(new MyListener(), sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
public void uiVars(){
baroText = (TextView) findViewById(R.id.pressureTxt);
timeText = (TextView) findViewById(R.id.timeTxt);
refresh = (Button) findViewById(R.id.baroRefreshBtn);
}
class MyListener implements SensorEventListener {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO
}
@Override
public void onSensorChanged(SensorEvent event) {
long timestamp = event.timestamp;
float value = event.values[0];
// do something with the values
}
}
}
I understand its likely something silly I am missing but I just wanted to get a second set of eyes looking at it. - Also if its any help I am running Android Studio as my IDE.
This should work:
@Override
public void onSensorChanged(SensorEvent event) {
long timestamp = event.timestamp;
float value = event.values[0];
String valueString = String.valueOf(value);
baroText.setText(valueString);
}
If what you want is display the value in this TextView.