How to get phone number from an incoming call?
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);
}
}
How to get incoming call number? [Android]
Please note you need permission READ_CALL_LOG
if you want to get incomingNumber
, you can check documents here
String: call phone number. If application does not have READ_CALL_LOG permission or carrier privileges (see TelephonyManager#hasCarrierPrivileges), an empty string will be passed as an argument.
Get incoming call number programmatically on android
Try this way...
permission
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
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>
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);
}
}
Get incoming call number programmatically on android 10
Yes, implement the class and add the necessary permission below in the manifest:
<service
android:name=".CallScreeningService"
android:permission="android.permission.BIND_SCREENING_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallScreeningService" />
</intent-filter>
</service>
Within onScreenCall(Call.Details details)
you can call details.getHandle()
which returns the telephone number of the incoming call. This will only get called if the number cannot be matched to the contact information existing on the device.
@Override
public void onScreenCall(Call.Details details) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if(details.getCallDirection() == Call.Details.DIRECTION_INCOMING) {
CallResponse.Builder response = new CallResponse.Builder();
response.setDisallowCall(false);
response.setRejectCall(false);
response.setSilenceCall(false);
response.setSkipCallLog(false);
response.setSkipNotification(false);
details.getHandle(); //This is the calling number
respondToCall(details, response.build());
}
}
}
Retrieve incoming call's phone number in Android
Use PhoneStateListener
. It has an onCallStateChanged
handler; one of the supplied arguments you'll get is a String
containing the incoming phone number.
How to detect incoming call phone number in Android when app is killed?
Not exactly an answer, but you can create your custom calling app and then make it default by asking for permission. A demo app can be found on this repository. Look at this app code and you might get it.
On Android how do I get the phone number from incoming call?
In order to get this to work in newer versions of Android (4.0.3 etc) you need to make sure that your minSdkVersion is 3.. The issue with my code was that my minSdkVersion was 7..
Hope this helps others trying to figure this out! :)
Related Topics
Date and Time Change Listener in Android
Android Fragment Onattach() Deprecated
Which Is the Best Way to Add a Button
Viewpager as a Circular Queue/Wrapping
How to Implement Multi-Select in Recyclerview
Android - Java.Lang.Securityexception: Permission Denial: Starting Intent
Android: How to Check If Activity Is Running
Override Home and Back Button Is Case a Boolean Is True
Difference Between Service, Async Task & Thread
How to Get the Touch Position in Android
Why Extend the Android Application Class
How to Set a Gradient Background in a Material Button from Material Components
How to Execute Async Task Repeatedly After Fixed Time Intervals
How to Connect Android App to MySQL Database