Retrieve incoming call's phone number in Android Retrieve incoming call's phone number in Android android android

Retrieve incoming call's phone number in Android


Use PhoneStateListener. It has an onCallStateChanged handler; one of the supplied arguments you'll get is a String containing the incoming phone number.


Your overridden method in CustomPhoneStateListener should be called onCallStateChanged() (and not onCallStateChange()).

This would have been spotted by the Java compiler if you would have had the @Override annotation, like you have for onReceive().


The above answers are out-od-dated now. There are valid for Android 7 and lower.

For android 9 and higher you have to add another permission in the Androidmanifest.xml with the permission

<uses-permission android:name="android.permission.READ_CALL_LOG"/>

without the phone number will be null. For Android 8 I am not sure.

PhoneStateReciever.java

    package com.incomingcalls;    import android.content.BroadcastReceiver;    import android.content.Context;    import android.content.Intent;    import android.telephony.TelephonyManager;    import android.util.Log;    import android.widget.Toast;    public class PhoneStateReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        try {            System.out.println("Receiver start");            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);            Log.e("Incoming Number", "Number is ," + incomingNumber);            Log.e("State", "State is ," + state);            if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){                Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();                Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();            }            if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){                Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();            }            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){                Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();            }        }        catch (Exception e){            e.printStackTrace();        }    }}

AnroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>    <manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.incomingcalls">    <uses-permission android:name="android.permission.READ_PHONE_STATE" />    <uses-permission android:name="android.permission.READ_CALL_LOG" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity            android:name=".MainActivity"            android:label="@string/app_name"            android:theme="@style/AppTheme.NoActionBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver android:name=".PhoneStateReceiver">            <intent-filter>                <action android:name="android.intent.action.PHONE_STATE" />            </intent-filter>        </receiver>    </application></manifest>