Get incoming call number programmatically on android 10

Tags: ,



Looking at previous solutions for this problem are now depreciated as of api 29(Android 10). Has anyone been able to get the incoming phone number on for api 29. Apparently now to do this you need to use CallScreeningService

Answer

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

        }
    }
}


Source: stackoverflow