title: sensorManager.registerListener in a background service
body: <p>I have an Android background service written for a Cordova app, which I want to interact with the accelerometer via the sensorManager.</p>
  
  <p>I feel I'm pretty close, but just not quite there, I think.</p>
  
  <p>For some reason the sensorManager.registerListener doesn't seem to fire the onSensorChanged method - I'm checking the X value every 5 seconds from the MyService (my background service class).</p>
  
  <p>From what I have read it's something to do with needing a handle on the registerListener, but I'm not sure...what or how.</p>
  
  <p>Here's my code (cut down for readability):</p>
  
  <pre><code>    class AccelListener implements SensorEventListener
      {
  
          public AccelListener(Context ctx) {
              mCtx = ctx;
              sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
          }
  
          public int start() {
               List&lt;Sensor&gt; list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
  
              if ((list != null) &amp;&amp; (list.size() &gt; 0)) {
                this.mSensor = list.get(0);
                this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI);
                 } 
  
              return this.status;
          }
  
          public void onSensorChanged(SensorEvent event) {
  
  
              if (this.accuracy &gt;= SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) {
                  this.timestamp = System.currentTimeMillis();
                  this.x = event.values[0];
                  this.y = event.values[1];
                  this.z = event.values[2];
              }
          }
      }
  
      public class MyService extends BackgroundService 
      {
  
          protected JSONObject doWork() {
              JSONObject result = new JSONObject();
              Context thisContext = getApplicationContext();
              AccelListener objAccel = new AccelListener(thisContext);
              int intAccelReturn = objAccel.start();
              setInterval(function()
              {
                  result.put("OUTPUT", objAccel.x);
              },5000);
  
  }
  </code></pre>
  
  <p>Would really appreciate any help.</p>
  
  <p>Thanks</p>
  
  <p>O.</p>