I dispositivi con Android 10 (livello API 29) o versioni successive consentono alla tua app di identificare come potenziali chiamate indesiderate le chiamate provenienti da numeri non presenti nella rubrica dell'utente. Gli utenti possono scegliere di rifiutare automaticamente le chiamate indesiderate. Per garantire una maggiore trasparenza agli utenti quando perdono chiamate, le informazioni su queste chiamate bloccate vengono registrate nel registro chiamate. L'utilizzo dell'API Android 10 elimina il
requisito di ottenere l'autorizzazione
READ_CALL_LOG
da parte dell'utente per fornire le funzionalità di filtro delle chiamate e ID chiamante.
Utilizzi un'implementazione di CallScreeningService
per filtrare le chiamate. Chiama la funzione onScreenCall()
per tutte le nuove chiamate in arrivo o in uscita quando il numero non è nell'elenco contatti dell'utente. Puoi controllare l'oggetto Call.Details
per informazioni sulla chiamata. In particolare, la funzione getCallerNumberVerificationStatus()
include informazioni del fornitore di rete relative all'altro numero.
Se lo stato della verifica non è riuscito, è un buon segnale che la chiamata proviene da un numero non valido o da una potenziale chiamata indesiderata.
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 } } } }
Imposta la funzione onScreenCall()
su respondToCall()
per indicare al sistema come rispondere alla nuova chiamata. Questa funzione utilizza un parametro CallResponse
che puoi utilizzare per indicare al sistema di bloccare la chiamata, rifiutarla come se l'utente lo avesse fatto o silenziarla. Puoi anche indicare al sistema di saltare l'aggiunta
di questa chiamata al registro chiamate del dispositivo.
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());
Devi registrare l'implementazione CallScreeningService
nel file manifest con l'autorizzazione e il filtro per intent appropriati in modo che il sistema possa attivarla correttamente.
<service
android:name=".ScreeningService"
android:permission="android.permission.BIND_SCREENING_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallScreeningService" />
</intent-filter>
</service>