0

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.

Chris Panella
  • 202
  • 1
  • 9
  • your first attempt result in recursion since you called it on top, second attempt whenever sensor changed it will create a new Handler so if the sensor changed rapidly result in many instances of handler, Try to Create Single Handler Object and postdelay at bottom. – Rachit Solanki May 17 '16 at 12:40
  • So would I only wrap the if statement in the Handler/Runnable? I believe I have tried that as well with only the same result in a delay in the first post of the label. – Chris Panella May 18 '16 at 03:17

1 Answers1

0

Try this First approach

  Handler mHandler = new Handler();
  float lux,conversion;// intialize it
  Runnable run=new Runnable() {

    @Override
    public void run() {

            textLightReading.setText("Light: " + conversion);
            //Line to kill runnable before a new one starts
            mHandler.removeCallbacks(run);

    }
};

  //it will update 1 second after onSensorChanged called and when condition true
  //As per your both attempts
  @Override
public void onSensorChanged(final SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_LIGHT) {

     lux = event.values[0];
     conversion = Math.round(((1/638f) * lux) * 100.0f)/100.0f;
    mHandler.postDelayed(run, 1000);

  }
}

To get the label to update every 'n' seconds doesn't matter if onSensorChanged called

second Approach Part 1 : //Use of Timer

 Timer swipeTimer = new Timer();
 swipeTimer.schedule(new TimerTask() {

            @Override
            public void run() {
                mHandler.post(run);
            }
        }, 500, 500); //put this onCreateView or at initialization

Note : When a timer is no longer needed, users should call cancel, which releases the timer's thread and other resources. Timers not explicitly cancelled may hold resources indefinitely.

second Approach Part 2: Similar approach mention in your question

  Handler mHandler = new Handler();
  Runnable run=new Runnable() {

    @Override
    public void run() {


            textLightReading.setText("Light: " + conversion); //update textView 
              mHandler.postDelayed(run, 1000);
    }
};


              mHandler.post(run); // at initialization or anywhere in your code where you want to start handler

  @Override
public void onSensorChanged(final SensorEvent event) {

if (event.sensor.getType() == Sensor.TYPE_LIGHT) {

     lux = event.values[0];
     conversion = Math.round(((1/638f) * lux) * 100.0f)/100.0f;

  }
}

There are many other ways you can do this...

Chris Panella
  • 202
  • 1
  • 9
Rachit Solanki
  • 369
  • 6
  • 13
  • 1
    I edited you answer but once I added 'handler.removeCallbacks(run);' to the run Runnable at the end it worked! It killed the current runnable before calling another new one 1sec after! Thank you so much for your help! – Chris Panella May 21 '16 at 04:34