title: Android PhoneStateListener onPhoneStateChanged method not invoked from service
body: <p>I am working on a app that runs in the background and listens to incoming calls.</p>
  
  <p>For this I have created a service which calls TelephonyManager.listen in the onHandleIntent method.</p>
  
  <p>Unfortunately although the constructor of the phonestatelistener is invoked, its onPhoneStateChanged method is not getting invoked.</p>
  
  <p>Making the same call from a activity works fine. I am confused what the issue might be. I have searched many similar posts but none of them have answered my question satisfactorily. Hence I am posting this question.</p>
  
  <p>Following is my service and phonelistener implementation:</p>
  
  <pre><code>public class PhoneListenersService extends IntentService{
  
  TelephonyManager tm;
  CallStateListener callstatelistener;
  public PhoneListenersService() {
      super("PhoneListenersService");
      // TODO Auto-generated constructor stub
  }
  
  @Override
  protected void onHandleIntent(Intent intent) {
      // TODO Auto-generated method stub      
      int count=0;
      do
      {
          TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
          TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
          Log.d("Count", ""+count++);
          try {
              Thread.sleep(100);
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      while(count&lt;100);
  }
  
   class TeleListener extends PhoneStateListener {
  
       public void onCallStateChanged(int state, String incomingNumber) {
             super.onCallStateChanged(state, incomingNumber);
             switch (state) {
             case TelephonyManager.CALL_STATE_IDLE:
              // CALL_STATE_IDLE;
              Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
                Toast.LENGTH_LONG).show();
              break;
             case TelephonyManager.CALL_STATE_OFFHOOK:
              // CALL_STATE_OFFHOOK;
              Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
                Toast.LENGTH_LONG).show();
              break;
             case TelephonyManager.CALL_STATE_RINGING:
              // CALL_STATE_RINGING
              Toast.makeText(getApplicationContext(), incomingNumber,
                Toast.LENGTH_LONG).show();
              Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
                Toast.LENGTH_LONG).show();
              break;
             default:
              break;
             }
            }
  
           }
  </code></pre>
  
  <p>}</p>
  
  <p>Please help me.</p>
  
  <p>Thanks!</p>