Identifier l'appelant

Les appareils équipés d'Android 10 (niveau d'API 29) ou version ultérieure permettent à votre application d'identifier les appels provenant de numéros ne figurant pas dans le carnet d'adresses de l'utilisateur comme des appels potentiellement indésirables. Les utilisateurs peuvent faire en sorte que les appels indésirables soient rejetés en mode silencieux. Pour assurer une plus grande transparence aux utilisateurs lorsqu'ils manquent des appels, les informations sur ces appels bloqués sont consignées dans le journal d'appels. Avec l'API Android 10, il n'est pas nécessaire d'obtenir l'autorisation READ_CALL_LOG de la part de l'utilisateur pour permettre le filtrage des appels et l'affichage du numéro de l'appelant.

Vous utilisez une implémentation CallScreeningService pour filtrer les appels. Appelez la fonction onScreenCall() pour tout nouvel appel entrant ou sortant lorsque le numéro ne figure pas dans la liste de contacts de l'utilisateur. Vous pouvez consulter l'objet Call.Details pour obtenir des informations sur l'appel. Plus précisément, la fonction getCallerNumberVerificationStatus() inclut des informations de la part du fournisseur d'accès sur l'autre numéro. Si l'état de la validation a échoué, cela signifie que l'appel provient d'un numéro non valide ou est potentiellement indésirable.

Kotlin

class ScreeningService : CallScreeningService() {
    // This function is called when an ingoing or outgoing call
    // is from a number not in the user's contacts list
    override fun onScreenCall(callDetails: Call.Details) {
        // Can check the direction of the call
        val isIncoming = callDetails.callDirection == Call.Details.DIRECTION_INCOMING

        if (isIncoming) {
            // the handle (e.g. phone number) that the Call is currently connected to
            val handle: Uri = callDetails.handle

            // determine if you want to allow or reject the call
            when (callDetails.callerNumberVerificationStatus) {
                Connection.VERIFICATION_STATUS_FAILED -> {
                    // Network verification failed, likely an invalid/spam call.
                }
                Connection.VERIFICATION_STATUS_PASSED -> {
                    // Network verification passed, likely a valid call.
                }
                else -> {
                    // Network could not perform verification.
                    // This branch matches Connection.VERIFICATION_STATUS_NOT_VERIFIED.
                }
            }
        }
    }
}

Java

class ScreeningService extends CallScreeningService {
    @Override
    public void onScreenCall(@NonNull Call.Details callDetails) {
        boolean isIncoming = callDetails.getCallDirection() == Call.Details.DIRECTION_INCOMING;

        if (isIncoming) {
            Uri handle = callDetails.getHandle();

            switch (callDetails.getCallerNumberVerificationStatus()) {
                case Connection.VERIFICATION_STATUS_FAILED:
                    // Network verification failed, likely an invalid/spam call.
                    break;
                case Connection.VERIFICATION_STATUS_PASSED:
                    // Network verification passed, likely a valid call.
                    break;
                default:
                    // Network could not perform verification.
                    // This branch matches Connection.VERIFICATION_STATUS_NOT_VERIFIED
            }
        }
    }
}

Définissez la fonction onScreenCall() pour qu'elle appelle respondToCall() afin d'indiquer au système comment répondre au nouvel appel. Cette fonction utilise un paramètre CallResponse que vous pouvez utiliser pour indiquer au système de bloquer l'appel, de le rejeter comme si l'utilisateur l'avait fait ou de le mettre sous silence. Vous pouvez également demander au système d'ignorer l'ajout de cet appel au journal d'appels de l'appareil.

Kotlin

// Tell the system how to respond to the incoming call
// and if it should notify the user of the call.
val response = CallResponse.Builder()
    // Sets whether the incoming call should be blocked.
    .setDisallowCall(false)
    // Sets whether the incoming call should be rejected as if the user did so manually.
    .setRejectCall(false)
    // Sets whether ringing should be silenced for the incoming call.
    .setSilenceCall(false)
    // Sets whether the incoming call should not be displayed in the call log.
    .setSkipCallLog(false)
    // Sets whether a missed call notification should not be shown for the incoming call.
    .setSkipNotification(false)
    .build()

// Call this function to provide your screening response.
respondToCall(callDetails, response)

Java

// Tell the system how to respond to the incoming call
// and if it should notify the user of the call.
CallResponse.Builder response = new CallResponse.Builder();
// Sets whether the incoming call should be blocked.
response.setDisallowCall(false);
// Sets whether the incoming call should be rejected as if the user did so manually.
response.setRejectCall(false);
// Sets whether ringing should be silenced for the incoming call.
response.setSilenceCall(false);
// Sets whether the incoming call should not be displayed in the call log.
response.setSkipCallLog(false);
// Sets whether a missed call notification should not be shown for the incoming call.
response.setSkipNotification(false);

// Call this function to provide your screening response.
respondToCall(callDetails, response.build());

Vous devez enregistrer l'implémentation de CallScreeningService dans le fichier manifeste avec le filtre d'intent et l'autorisation appropriés pour que le système puisse la déclencher correctement.

<service
    android:name=".ScreeningService"
    android:permission="android.permission.BIND_SCREENING_SERVICE">
    <intent-filter>
        <action android:name="android.telecom.CallScreeningService" />
    </intent-filter>
</service>