title: Phone state listener using broadcast receiver in android not working
body: <p>I am trying to detect Incoming call using <code>Broadcast Listener</code>.Since I received one fault in my app when someone calls, my app still plays the song. So I want to pause song during Incoming Call. But I am not receiving any response.</p>
  
  <p>Here is the complete <code>code</code> So that you all can understand where am I messing.</p>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:supportsRtl="true"
      android:theme="@style/AppTheme"&gt;
  
      &lt;activity android:name=".MainActivity"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;receiver android:name=".IncomingCall"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.PHONE_STATE" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>IncomingCall.java</strong></p>
  
  <pre><code>package com.example.suraj.freewee;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.telephony.PhoneStateListener;
  import android.telephony.TelephonyManager;
  import android.util.Log;
  
  public class IncomingCall extends BroadcastReceiver {
  
  private String LOG_TAG = getClass().getSimpleName();
  TelephonyManager telephonyManager;
  Context context;
  
  @Override
  public void onReceive(Context context, Intent intent) {
      try {
          this.context = context;
          telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
          MyPhoneStateListener phoneListener = new MyPhoneStateListener();
          telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
  
          Log.e(LOG_TAG, "inside on receive");
      } catch (Exception e) {
          e.printStackTrace();
          Log.e(LOG_TAG, e.toString());
      }
  
  
  }
  
  private class MyPhoneStateListener extends PhoneStateListener {
      @Override
      public void onCallStateChanged(int state, String incomingNumber) {
          Log.e(LOG_TAG, "Number : " + incomingNumber);
          try {
              switch (state) {
                  case TelephonyManager.CALL_STATE_RINGING: {
                      //PAUSE
                      SongsAdapter.player.pause();
                      Log.e(getClass().getSimpleName(), "call ringing");
                      break;
                  }
                  case TelephonyManager.CALL_STATE_OFFHOOK: {
                      SongsAdapter.player.pause();
                      Log.e(getClass().getSimpleName(), "call offhook");
                      break;
                  }
                  case TelephonyManager.CALL_STATE_IDLE: {
                      //PLAY
                      SongsAdapter.player.start();
                      Log.e(getClass().getSimpleName(), "call idle");
                      break;
                  }
                  default: {
                  }
              }
          } catch (Exception ex) {
              Log.e(getClass().getSimpleName(), "Error: " + ex.toString());
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>In <code>logcat</code> no log errors are shown
  So why is this giving me such behaviour. thanks in advance</p>