Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get phone number from an incoming call?

Tags:

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

like image 443
Saqib Abbasi Avatar asked Oct 31 '12 09:10

Saqib Abbasi


People also ask

How do I find out the number of calls received?

See your call historyTap Recents . You'll see one or more of these icons next to each call in your list: Missed calls (incoming) (red) Calls you answered (incoming) (blue)


1 Answers

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);
    }
}
like image 116
Mohammed Azharuddin Shaikh Avatar answered Sep 20 '22 17:09

Mohammed Azharuddin Shaikh