How do I get the phone number when there is an incoming call in Android?
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)
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With