title: Android: PhoneStateListener not working in Service
body: <p>I'm trying to build an application that logs when I accept and reject calls. Therefor I'm using a PhoneStateListener.</p>
  
  <p>When I start the listener in the <code>onCreate()</code> method it is stopping its activity after some time. As far as I know Android closes the App because it has no focus.</p>
  
  <p>I tried to work around that behavior with starting a service. All the code I wrote works fine and doesn't get killed by android...but the PhoneStateListener doesn't recieve any events.</p>
  
  <p>How I start the Service:</p>
  
  <pre><code>public class RunningService extends IntentService {
  
      /**
       * A constructor is required, and must call the super IntentService(String)
       * constructor with a name for the worker thread.
       */
      public RunningService() {
          super("RunningService");
      }
  
      /**
       * The IntentService calls this method from the default worker thread with
       * the intent that started the service. When this method returns,
       * IntentService stops the service, as appropriate.
       */
      @Override
      protected void onHandleIntent(Intent intent) {
          //...some code...creating a notification
          startForeground(12345, notification);
  
          TelephonyManager telephonyManager = (TelephonyManager) this
                  .getSystemService(TELEPHONY_SERVICE);
          telephonyManager.listen(new PhoneStateListenerImpl(),
                  PhoneStateListener.LISTEN_CALL_STATE);
  
          while (true) {
              ...some code to do...until Exit is called by user
          }
      }
  }
  </code></pre>
  
  <p>The PhoneStateListener with basic output:</p>
  
  <pre><code>public class PhoneStateListenerImpl extends PhoneStateListener {
  
      public PhoneStateListenerImpl() {
          super();
          Log.v("psListener", "constructor");
      }
  
      @Override
      public void onCallStateChanged(int state, String incomingNumber) {
  
          switch (state) {
          case TelephonyManager.CALL_STATE_IDLE:
              Log.v("PhoneStateListener", "IDLE");
              break;
          case TelephonyManager.CALL_STATE_OFFHOOK:
              Log.v("PhoneStateListener", "OFFHOOK");
              break;
          case TelephonyManager.CALL_STATE_RINGING:
              Log.v("State", "Ringing");
              break;
          default: {
              Log.v("Status", "something");
          }
          }
      }
  }
  </code></pre>
  
  <p>I get the Log output frome the Listeners constructor, but whenever something changes (like I'm calling someone) nothing happens.</p>
  
  <p>The same Listener started from <code>onCreate()</code> works fine.</p>
  
  <p>Maybe I'm missing something</p>
  
  <p>I just tried:
  I tried to ask the TelephonyManager for the phone state in the while loop of the Service. This works fine. But I guess this is just a dirty workaround.</p>
  
  <p>Has anybody an idea what could be the problem?</p>