title: How to log data from Android Motion Sensors at a fixed rate
body: <p>I'm learning the Basics of Android programming.</p>
  
  <p>I have a simple android test application in which i log the accelerometer,magnetometer and the orientation data to an external file while also displaying it. I initiate the logging process on click of a <strong>Start</strong> button (registerListener for relevant sensors) by calling a method <strong>initLogger</strong>. </p>
  
  <p>Which looks something similar to this...</p>
  
  <pre><code>public void initLogger(View view)
  {
      boolean bFlag = false;
  
      Button btnStart = (Button)findViewById(R.id.btnStartLog);
      Button btnStop = (Button)findViewById(R.id.btnStopLog);
  
      btnStart.setEnabled(bFlag);
      btnStop.setEnabled(!bFlag);
  
      bEnableLogging = true;
      //Start reading the sensor values
      sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI);
      sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
  
     //so on.... 
  </code></pre>
  
  <p>There is also a Stop button, which shall stop the logging process (and finally unregister by calling unregisterListener for each sensor)</p>
  
  <p>The data retrieval process happens inside the <strong>onSensorChanged</strong> handler which shall retrieve the data from the relevant sensors, sets the value to the respective UI elements and finally log the data to an external .csv file. </p>
  
  <p><strong>onSensorChanged</strong> eventhandler looks something like this ...</p>
  
  <pre><code>public void onSensorChanged(SensorEvent event) {
  
  
      // TODO Auto-generated method stub
      // accelerometer
      TextView tAX = (TextView) findViewById(R.id.txtViewAxValue);
      TextView tAY = (TextView) findViewById(R.id.txtViewAyValue);
      TextView tAZ = (TextView) findViewById(R.id.txtViewAzValue);
  
      // magnetic field
      TextView tMX = (TextView) findViewById(R.id.txtViewMx);
      TextView tMY = (TextView) findViewById(R.id.txtViewMy);
      TextView tMZ = (TextView) findViewById(R.id.txtViewMz);
  
      if (bEnableLogging) {
          if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
  
              accelerometerdata = event.values.clone();
  
              tAX.setText(Double.toString(accelerometerdata[0]));
              tAY.setText(Double.toString(accelerometerdata[1]));
              tAZ.setText(Double.toString(accelerometerdata[2]));
  
  
          }
  
          if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
  
              magneticmatrixdata = event.values.clone();
  
              tMX.setText(Double.toString(magneticmatrixdata[0]));
              tMY.setText(Double.toString(magneticmatrixdata[1]));
              tMZ.setText(Double.toString(magneticmatrixdata[2]));
  
          }
  
                 // so on ....
  </code></pre>
  
  <p>Although i receive the data from all the configured sensors, i do not have the control over the rate at which the data is received. i.e </p>
  
  <p>I know SensorChanged event is fired as and when the Sensor data is changed. However i want this event to be fired at a fixed rate. <strong>For ex:  every 40ms</strong></p>
  
  <p><strong>Question:</strong></p>
  
  <ol>
  <li>How to ensure the SensorChanged event is fired at constant rate ?</li>
  <li>Is the class TimerTask in Java of any help in this case ?</li>
  </ol>
  
  <p>Experts out here in SO.Please help me :)</p>