How do I get the phone number when there is an incoming call in Android?
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);
}
}