title: i want to get incoming call number but number is empty
body: <p>I want to get an incoming number whenever anyone is going to be called but getting empty even after use <code>PhoneStateListener and BroadCastReceiver</code>.</p>
  
  <p>First, clear my doubt that which one is the best to approach and why we use both <code>PhoneStateListener and BroadCastReceiver</code>.</p>
  
  <blockquote>
    <p><strong>Manifest File.xml</strong></p>
  </blockquote>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="mytraining.com.callrecord"&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;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  
      &lt;activity
          android:name=".MainActivity"
          android:label="@string/app_name"
          android:screenOrientation="portrait"&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=".PhoneReceiver"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.PHONE_STATE" /&gt;
              &lt;action android:name="android.intent.action.NEW_OUTGOING_CALL" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p></p>
  
  <blockquote>
    <p><strong>MainActivity.java</strong></p>
  </blockquote>
  
  <pre><code> public class MainActivity extends AppCompatActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }}
  </code></pre>
  
  <blockquote>
    <p><strong>BroadcastReceiver.java</strong></p>
  </blockquote>
  
  <pre><code>    public class PhoneReceiver extends BroadcastReceiver {
       private final String TAG = getClass().getName();
     private static String number = null;
  @Override
  public void onReceive(final Context context, Intent intent) {
      if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
          String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
          Log.d(TAG, intent.getAction() + ", EXTRA_STATE: " + state);
          // on ringing get incoming number
          if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
              number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
              Log.d(TAG, "EXTRA_INCOMING_NUMBER: " + number);
  
          }
      }
  
      if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
          number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
          Log.d(TAG, intent.getAction() + ", EXTRA_PHONE_NUMBER: " + number);
      }
  }}
  </code></pre>