title: Android sensors not working in a service
body: <p>I'm pretty new to android programming and following is my use case : 
  I have a single activity 'MainActivity' and a service 'MyService'. I call my service using <code>startService</code> method. In my MyService.java, I register for 2 different sensors (`accelerometer and light) and try to get sensor values.
  This seems pretty basic and I code it the following way : 
  MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity{
  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Intent i = new Intent(MainActivity.this, MyService.class);
  
          startService(i);
      }
  </code></pre>
  
  <p>MyService.java</p>
  
  <pre><code>public class MyService extends Service implements SensorEventListener{
      public MyService() {
      }
  
      private SensorManager sensorManager;    // this instance of SensorManager class will be used to get a reference to the sensor service.
      private Sensor mSensor,lSensor;         // this instance of Sensor class is used to get the sensor we want to use.
      private float[] mGravity;
      private float mAccel;
      private float mAccelCurrent;
      private float mAccelLast;
  
      public void onAccuracyChanged(Sensor sensor,int accuracy){
  
      }
  
      //    // if sensor value is changes, change the values in the respective textview.
      public void onSensorChanged(SensorEvent event){
          Log.d("sensorService", "onSensorChanged.");
          Toast.makeText(MyService.this, "onSensorChanged", Toast.LENGTH_SHORT).show();
  
          /* check sensor type */
          if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
  
              mGravity = event.values.clone();
              // assign directions
              float x=event.values[0];
              float y=event.values[1];
              float z=event.values[2];
  
              float x1=event.values[0];
              float y1=event.values[1];
              float z1=event.values[2];
              mAccelLast = mAccelCurrent;
              mAccelCurrent = (float)Math.sqrt(x*x + y*y + z*z);      // we calculate the length of the event because these values are independent of the co-ordinate system.
              float delta = mAccelCurrent - mAccelLast;
              mAccel = mAccel*0.9f + delta;
              if(mAccel &gt; 3)
              {
                  Toast.makeText(MyService.this, "Movement Detected.", Toast.LENGTH_SHORT).show();
              }
          }
          else if(event.sensor.getType()==Sensor.TYPE_LIGHT)
          {
              float l = event.values[0];
          }
  
          /* unregister if we just want one result. */
  //        sensorManager.unregisterListener(this);
  
      }
  
      @Override
      public void onCreate() {
          super.onCreate(); // if you override onCreate(), make sure to call super().
          // If a Context object is needed, call getApplicationContext() here.
          Log.d("MyService", "onCreate");
          sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);          // get an instance of the SensorManager class, lets us access sensors.
          mSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    // get Accelerometer sensor from the list of sensors.
          lSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);            // get light sensor from the list of sensors.
          mAccel = 0.00f;
          mAccelCurrent = SensorManager.GRAVITY_EARTH;
          mAccelLast = SensorManager.GRAVITY_EARTH;
  
      }
  
      public int onStartCommand(Intent intent, int flags, int startId){
          Log.d("MyService", "onStartCommand");
          sensorManager=(SensorManager) getSystemService(SENSOR_SERVICE);          // get an instance of the SensorManager class, lets us access sensors.
          mSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);    // get Accelerometer sensor from the list of sensors.
          lSensor = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);            // get light sensor from the list of sensors.
          mAccel = 0.00f;
          mAccelCurrent = SensorManager.GRAVITY_EARTH;
          mAccelLast = SensorManager.GRAVITY_EARTH;
          return START_STICKY;
      }
  
  
  
      @Override
      public IBinder onBind(Intent intent) {
          // TODO: Return the communication channel to the service.
          throw new UnsupportedOperationException("Not yet implemented");
      }
  }
  </code></pre>
  
  <p>Manifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.tyagi.smartalarm" &gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/web_hi_res_512"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name"
              android:theme="@style/AppTheme.NoActionBar" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name=".AlarmReceiver"
              android:process=":remote" &gt;
  
              &lt;!-- define android:process=":remote" so that the BroadcastReceiver will run in a separate process --&gt;
              &lt;!-- so that it will continue to stay alive if the app has closed. --&gt;
          &lt;/receiver&gt;
  
          &lt;service
              android:name=".sensorService"
              android:exported="false" /&gt;
          &lt;!-- exported determines whether or not the service can be executed by other applications. --&gt;
  
          &lt;service
              android:name=".MyService"
              android:enabled="true"
              android:exported="true" &gt;
          &lt;/service&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>This doesn't seem to work as the <code>onSensorChanged</code> method in MyService.java file doesn't get called. I can't really fathom where I'm going wrong. Any help is appreciated.
  Thanks in advance.</p>