I have a working app that I need adjusted. I am trying to get the label to update every 'n' seconds using the postDelayed method but I am only getting it to delay the time in which it first shows versus updating the label every so often. I have tried multiple ways of using the Runnable method with postDelayed but I haven't been able to do anything except, again, delay the initial post. I would appreciate any feedback or advice as to how to get it to work. The "textLightReading.setText" is the label I am trying to delay the update for.
`@Override
public void onSensorChanged(final SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mHandler.postDelayed(this, 1000);
final float lux = event.values[0];
final float conversion = Math.round(((1/638f) * lux) * 100.0f)/100.0f;
textLightReading.setText("Light: " + conversion);
}
}, 1000);
}
}`
This is another attempt...
`@Override
public void onSensorChanged(final SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
float lux = event.values[0];
final float conversion = Math.round(((1/638f) * lux) * 100.0f)/100.0f;
final Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
textLightReading.setText("Light: " + conversion);
//mHandler.postDelayed(this, 1000);
}
}, 1000);
}
}`
I am sure I just don't understand how to use this. Thanks again in advance.