title: Android Phone_State not getting incoming calls
body: <p>The problem in my work is if I give the <code>android.intent.action.PHONE_STATE</code> intent from the shell it works but does not get the phone number. But the main problem is that when I call my phone it does not get any effect. What is the problem when I call my phone with another phone when the app is closed, why not receive this call?</p>
  <p><strong>Here is AndroidManifest.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      package=&quot;homework.contactor&quot;&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot;/&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/AppTheme&quot;&gt;
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;receiver android:name=&quot;.ServiceReceiver&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.PHONE_STATE&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p><strong>Here is the Receiver Code:</strong></p>
  <pre><code>package homework.contactor;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.telephony.PhoneStateListener;
  import android.telephony.TelephonyManager;
  import android.util.Log;
  
  class MyPhoneStateListener extends PhoneStateListener {
  
      public static Boolean phoneRinging = false;
  
      public void onCallStateChanged(int state, String incomingNumber) {
  
          switch (state) {
              case TelephonyManager.CALL_STATE_IDLE:
                  Log.d(&quot;DEBUG&quot;, &quot;IDLE&quot;);
                  phoneRinging = false;
                  break;
              case TelephonyManager.CALL_STATE_OFFHOOK:
                  Log.d(&quot;DEBUG&quot;, &quot;OFFHOOK&quot;);
                  phoneRinging = false;
                  break;
              case TelephonyManager.CALL_STATE_RINGING:
                  Log.d(&quot;DEBUG&quot;, &quot;RINGING&quot;);
                  phoneRinging = true;
  
                  break;
          }
      }
  }
  
  public class ServiceReceiver extends BroadcastReceiver {
      TelephonyManager telephony;
  
      public void onReceive(Context context, Intent intent) {
          MyPhoneStateListener phoneListener = new MyPhoneStateListener();
          telephony = (TelephonyManager) context
                  .getSystemService(Context.TELEPHONY_SERVICE);
          telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
      }
  
      public void onDestroy() {
          telephony.listen(null, PhoneStateListener.LISTEN_NONE);
      }
  
  }
  </code></pre>