title: Repeat a task after exact n Millisecond in android
body: <p>My requirement is to sample android sensor data after every <em>n</em> millisecond exact.</p>
  
  <p>Tried using <code>timer</code> , <code>alarmManager</code> and <code>handler</code>. The task getting repeated but the duration is not exact <em>n</em> millisecond.</p>
  
  <p>Code::</p>
  
  <p>Handle:</p>
  
  <pre><code>Handler sensorHandler = new Handler();
      private Runnable sensorRunnable = new Runnable() {
          @Override
          public void run() {
              sendAllSensorData();
              sensorHandler.postDelayed(sensorRunnable, 50);
          }
      };
  
      private void postSensorMsgHandler() {
          sensorHandler.postDelayed(sensorRunnable, 50);
      }
  </code></pre>
  
  <p>Timer ::
  private void initTimer() {</p>
  
  <p>timer = new Timer();</p>
  
  <pre><code>timer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
          sendAllSensorData();
      }
  }, TelemetricConstant.delay, TelemetricConstant.SAMPLE_PERIOD);
  </code></pre>
  
  <p>}</p>
  
  <p>Logs::</p>
  
  <pre><code>06-22 15:34:53.201 D/Telemetric(27434): Telemetric Gyroscope Y mean = -0.000533 sd = 0.000958 variance = 0.000001
  
  06-22 15:34:53.212 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.001057 sd = 0.000000 variance = 0.000000
  
  06-22 15:34:53.253 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.000524 sd = 0.000754 variance = 0.000001
  
  06-22 15:34:53.264 D/Telemetric(27434): Telemetric Gyroscope Y mean = -0.000009 sd = 0.001066 variance = 0.000001
  
  06-22 15:34:53.306 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.000524 sd = 0.001376 variance = 0.000002
  
  06-22 15:34:53.317 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.000844 sd = 0.001390 variance = 0.000002
  
  06-22 15:34:53.361 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.000702 sd = 0.001291 variance = 0.000002
  
  06-22 15:34:53.370 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.000600 sd = 0.001209 variance = 0.000001
  
  06-22 15:34:53.414 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.000391 sd = 0.001266 variance = 0.000002
  
  06-22 15:34:53.423 D/Telemetric(27434): Telemetric Gyroscope Y mean = 0.000465 sd = 0.001205 variance = 0.000001
  </code></pre>