3

i want to print accelerometer values every 3 seconds. this is my code so far

@Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub

        if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER)
        {              
            x = a * x + (1 - a) * event.values[0];
            y = a * y + (1 - a) * event.values[1];
            z = a * z + (1 - a) * event.values[2];
              new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {                     
                      printValue();
                  }
              }, 3000);                        
        }

    }

it's delaying output only when application launch ,what is my mistake and how to solve it?

Roy
  • 398
  • 1
  • 6
  • 20

5 Answers5

3

You need to remove your Handler from onSensorChanged() so it will run constantly. Right now it will only execute if you have a change in your sensor reading. Also, you need to add a call to rerun the Runnable again, otherwise it will only execute once.

//Outside of onSensorChanged(), perhaps in onCreate() of your Activity
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        printValue();
        new Handler().postDelayed(this, 3000);
    }
}, 3000);
Dan Harms
  • 4,725
  • 2
  • 18
  • 28
  • thanks, it works for my case , for this case i'm using your solution, thanks :D – Roy Oct 08 '14 at 16:02
0

I am not too familiar with postDelayed, but I would hazard a guess that each of the calls to run are being delayed by 3 seconds, however each delay is being counted in parallel, where as you are looking to have them delayed sequentially so to speak.

Depending on how often onSensorChanged is called you might be better off doing something like checking the elapsed time since the last call, and if > 3 seconds then running printValue

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if(/*time elapsed > 3 seconds*/)
        if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER)
        {              
            x = a * x + (1 - a) * event.values[0];
            y = a * y + (1 - a) * event.values[1];
            z = a * z + (1 - a) * event.values[2];
              new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {                     
                      printValue();
                  }
             }, 3000);                        
        }
    }
}
Stephen
  • 4,041
  • 22
  • 39
  • Nope, this will print after 3 seconds all values, and having the temporal check outside the sensor type check is incorrect, as the values won't update. during this time – Federico Picci Aug 03 '18 at 08:24
0

I would personally use a thread for this, you can have this start up in the onCreate so its always running and so your not waiting for it to start when onSensorChanged happens.

Thread printvalue = new Thread(new Runnable() {
  @Override
  public void run() {
    try {
      printValue();
      Thread.sleep(3000);
    } catch (Exception e) {
      e.printstacktrace();
    }
  }
});
apmartin1991
  • 3,064
  • 1
  • 23
  • 44
0

What your current code is doing is waiting for a change in the sensor, then waiting 3sec and printing.

If you want to print every 3seconds you need to re-post your runnable when it's done. postDelayed is just delaying before execution, you need to start another 'timer' of 3seconds when you have done your print.

check here : Android - running a method periodically using postDelayed() call

there is a call to postDelayed(runnable) inside the run method of the runnable itself.

Community
  • 1
  • 1
Xavier Falempin
  • 1,186
  • 7
  • 19
0

This will print the value if was updated after 3 seconds. So, it could happen after 3 seconds and some milliseconds, but you don't have to manage a different thread.

private final static long ACC_CHECK_INTERVAL = 3000;
private long lastAccCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    lastAccCheck = System.currentTimeMillis();
}

@Override
public void onSensorChanged(SensorEvent event) {
    long currTime = System.currentTimeMillis();
    if( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){              
        x = a * x + (1 - a) * event.values[0];
        y = a * y + (1 - a) * event.values[1];
        z = a * z + (1 - a) * event.values[2];

        if(currTime - lastAccCheck > ACC_CHECK_INTERVAL) {
            printValue();
        }
    }

}
Federico Picci
  • 1,105
  • 10
  • 17