title: BroadcastReceiver with a Listener drains battery when not in use
body: <p>I have a receiver which waits for <code>TelephonyManager.ACTION_PHONE_STATE_CHANGED</code>:</p>
  
  <pre><code>public void onReceive(Context context, Intent intent) {
  String theAction = intent.getAction();  
  if (theAction != null &amp;&amp; theAction.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
          CONTEXT = context;
          startListening();   
  }
  </code></pre>
  
  <p>When it gets it, I register the listener (for orientation sensor):</p>
  
  <pre><code>public void startListening() {  
      sensorManager = (SensorManager) CONTEXT.getSystemService(Context.SENSOR_SERVICE);
      List&lt;Sensor&gt; sensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
      if (sensors.size() &gt; 0) {
          sensor = sensors.get(0);
          running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_FASTEST);
      }
  }   
  </code></pre>
  
  <p>And then, in the Listener's <code>onSensorChanged()</code> method:</p>
  
  <pre><code>public void onSensorChanged(SensorEvent event) {
      if(running) {   
          sensorManager.unregisterListener(this);
          // do some not significant stuff here
      }
  </code></pre>
  
  <p>And now the problem: I get a phone call, receiver starts, then listener starts and everything works as designed. But, later on, when I look at the battery stats, my app is listed as the one with 2-3% of drain. <em>And</em> this percentage is increasing <em>even though</em> there are no calls - hence the receiver <em>is not</em> working.</p>
  
  <p>Where does this battery drain come from? The activity which starts the receiver is not working (and it is single checkbox anyway). I suppose, that the listener may not get unregistered by <code>sensorManager.unregisterListener(this);</code>, but it shoudl work - I unregister the listener when it received the first event, it makes sense.</p>
  
  <p>How could I solve the problem?</p>