How to get phone number from an incoming call?

Saqib Abbasi picture Saqib Abbasi · Oct 31, 2012 · Viewed 54.3k times · Source

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

Answer

Mohammed Azharuddin Shaikh picture Mohammed Azharuddin Shaikh · Oct 31, 2012

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);
    }
}