title: How to detect device is moving or not using sensor android
body: <p>I am using below code to identify the movement of the device, means I would like to know that device is moving or not. I also use Google Activity APIs which provides different activity modes like WALKING, ON_FOOT, STILL, etc without using GPS. I would like to achieve the same with Sensors but I am not able to get it accurately.</p>
  
  <p>The issue with the following code is that as soon as I move the device quickly like take it from the table then I am getting the result as moving whereas it's not actually moving.</p>
  
  <pre><code>// calling method from onSensorChanged method and using TYPE_ACCELEROMETER sensor.
  double speed = getAccelerometer(event.values);
  
  // then checking the speed.
  if(speed &gt; 0.9 &amp;&amp; speed &lt; 1.1) {
     // device is not moving
  } else {
     // device is moving.
  }
  
  /**
       * @return
       */
      private double getAccelerometer(float[] values) {
          // Movement
          float x = values[0];
          float y = values[1];
          float z = values[2];
  
          float accelerationSquareRoot =
                  (float) ((x * x + y * y + z * z) / (9.80665 * 9.80665));
  
          return Math.sqrt(accelerationSquareRoot);
      }
  </code></pre>
  
  <p>Can anyone guide me how to make this logic accurate so that I can identify the device is moving or not?</p>