title: Can't access the sensors in Android
body: <p>I am writing an compass application but can't access the sensors needed for some reason. 
  I have implemented the <code>SensorEventListener</code> interface and register my sensors this way:</p>
  
  <pre><code>    sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
      accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
      magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  </code></pre>
  
  <p>In my <code>onSensorChange()</code> method:</p>
  
  <pre><code>public void onSensorChanged(SensorEvent event) {
      Toast t = Toast.makeText(this, "onSensorChanged", Toast.LENGTH_LONG);
      t.setGravity(Gravity.TOP, 0, 0);
      t.show();
      if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
          mGravity = event.values;
      }
      if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
          mGeomagnetic = event.values;
      }
  
      if(mGravity !=null &amp;&amp; mGeomagnetic !=null){
          float [] R = new float[9];
          float [] I = new float [9];
          boolean success = sensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
          if(success){
              float [] orientation = new float [3];
                sensorManager.getOrientation(R, orientation);
              north = orientation[0];
          }
      }
  }
  </code></pre>
  
  <p>The <code>Toast</code> message won't show on the screen and when I try to put the north value in a <code>TextView</code> the output is <code>0</code>. Would be great if someone could explain to me why I can't access the <code>onSensorChanged()</code> method. Permissions in manifest:</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  </code></pre>