Search code examples
javaandroidrunnablepostdelayed

Using postDelayed for Sensor Data


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.


Solution

  • 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...