21

How do I get the phone number when there is an incoming call in Android?

Marc
  • 16,170
  • 20
  • 76
  • 119
Saqib Abbasi
  • 607
  • 2
  • 6
  • 17
  • you want phone number from recent call history or get when alert incoming call ? – ckpatel Oct 31 '12 at 09:11
  • @ChiragPatel from recent incoming call – Saqib Abbasi Oct 31 '12 at 09:26
  • 1
    there is a similar question [here][1] take a look [1]: http://stackoverflow.com/questions/1853220/retrieve-incoming-calls-phone-number-in-android – Antarix Nov 01 '12 at 08:06
  • Please refer to this question: [Retrieve incoming call's phone number in Android](https://stackoverflow.com/questions/1853220/retrieve-incoming-calls-phone-number-in-android) And also have a look at this answer too: [how do I get the phone number from incoming call?](https://stackoverflow.com/questions/8880508/on-android-how-do-i-get-the-phone-number-from-incoming-call) – Mohamed Anis Dahmani Oct 31 '12 at 09:12

7 Answers7

43

Make a Broadcast receiver say ServiceReceiver assign its action in Manifest.

<receiver android:name=".ServiceReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

Add a PhoneStateListener to your TelephonyManager, PhoneStateListener having override onCallStateChanged() with Incoming number parameter. Thats it.

ServiceReceiver.Java

public class ServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                System.out.println("incomingNumber : "+incomingNumber);
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }
}
zeeali
  • 1,524
  • 20
  • 31
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
6

Following solution helped me retrieve the incoming and outgoing phone numbers.

Things to include in manifest :

1) Permission :

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

2) A broadcast receiver:

<receiver android:name=".AnswerCallBroadcastReceiver">
        <intent-filter> 
            <action android:name="android.intent.action.PHONE_STATE" />
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
</receiver>

Things to include in BroadcastReceiver class :

    public class AnswerCallBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {

            if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){

                String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE);

                if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                    Log.d(TAG, "Inside Extra state off hook");
                    String number = arg1.getStringExtra(TelephonyManager.EXTRA_PHONE_NUMBER);
                    Log.e(TAG, "outgoing number : " + number);              
                }       

                else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){   
                    Log.e(TAG, "Inside EXTRA_STATE_RINGING");
                    String number = arg1.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    Log.e(TAG, "incoming number : " + number);
                }
                else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                    Log.d(TAG, "Inside EXTRA_STATE_IDLE");
                }   
            }   
        }
    }
Basher51
  • 1,319
  • 1
  • 17
  • 28
4

This will definitely help you.

Here is an implementation, which will allow you to retrieve the phone number if it is an incoming phone call as incoming Number, and also when the call is FINISHED - note the Handler() code.

private class PhoneCallListener extends PhoneStateListener {

private boolean isPhoneCalling = false;

@Override
public void onCallStateChanged(int state, String incomingNumber) {

    if (TelephonyManager.CALL_STATE_RINGING == state) {
        // phone ringing
        Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
    }

    if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
        // active
        Log.i(LOG_TAG, "OFFHOOK");

        isPhoneCalling = true;
    }

    if (TelephonyManager.CALL_STATE_IDLE == state) {
        // run when class initial and phone call ended, need detect flag
        // from CALL_STATE_OFFHOOK
        Log.i(LOG_TAG, "IDLE number");

        if (isPhoneCalling) {

            Handler handler = new Handler();

            //Put in delay because call log is not updated immediately when state changed
            // The dialler takes a little bit of time to write to it 500ms seems to be enough
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // get start of cursor
                      Log.i("CallLogDetailsActivity", "Getting Log activity...");
                        String[] projection = new String[]{Calls.NUMBER};
                        Cursor cur = getContentResolver().query(Calls.CONTENT_URI, projection, null, null, Calls.DATE +" desc");
                        cur.moveToFirst();
                        String lastCallnumber = cur.getString(0);
                }
            },500);

            isPhoneCalling = false;
        }

    }
} 

And then add and initialise the listener in your onCreate or onStartCommand code:

  PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) this
        .getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
        PhoneStateListener.LISTEN_CALL_STATE);
nsandersen
  • 896
  • 2
  • 16
  • 41
Chintan Khetiya
  • 15,962
  • 9
  • 47
  • 85
3

you can find solution here

Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
Community
  • 1
  • 1
Trần Sĩ Long
  • 457
  • 3
  • 15
2

If you are using InCallService and you have a Call object then you can quickly get the incoming phone number by using the following code


if (call.getDetails().getGatewayInfo()!=null){

number=call.getDetails().getGatewayInfo().getOriginalAddress().getSchemeSpecificPart();}

else {  
number=mainCall.getDetails().getHandle().getSchemeSpecificPart();}

Here the call is the object from android.telecom.Callpackage and the 'number' is a string type variable.

Hope this is helpful for you.

1

The answers above are right, but, don't forget about permissions
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

NOTE, starting form Android 9 to read incoming call number you have to request this permission
<uses-permission android:name="android.permission.READ_CALL_LOG" />

Amjad Alwareh
  • 2,926
  • 22
  • 27
0

From Android 9 explicitly ask for two runtime permission.

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

Now inside onReceive we can extract phone number from intent

String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

But the catch is here: onReceive method is called twice for each state, one EXTRA_INCOMING_NUMBER with blank, and other populated with the phone number. So if you want to extract phone number you must get the correct value you can do this, just return which contain null ..

 @Override
public void onReceive(Context context, Intent intent) {
 if(!intent.hasExtra(TelephonyManager.EXTRA_INCOMING_NUMBER))
            return;
   //....Further TODO
}
Rohit Kumar
  • 51
  • 1
  • 6