Os dispositivos com o Android 10 (nível 29 da API) ou versões mais recentes permitem que o app identifique
ligações de números que não estão na lista de endereços do usuário como possíveis ligações
spam. Os usuários podem optar por rejeitar ligações spam de forma silenciosa. Para oferecer mais
transparência aos usuários quando eles perdem ligações, as informações sobre essas chamadas
bloqueadas são registradas no histórico de ligações. O uso da API do Android 10 elimina o
requisito de receber a permissão
READ_CALL_LOG
do usuário para oferecer a funcionalidade de filtro de ligações e identificador de
chamadas.
Use uma implementação de
CallScreeningService
para filtrar chamadas. Chame a função
onScreenCall()
para todas as novas ligações recebidas ou efetuadas quando o número não estiver na
lista de contatos do usuário. Verifique o objeto
Call.Details para ver informações
sobre a chamada. Especificamente, a função
getCallerNumberVerificationStatus()
inclui informações do provedor de rede sobre o outro número.
Se o status de verificação falhar, isso é um bom indício de que a chamada é
de um número inválido ou uma possível ligação spam.
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. } } } } }
Defina a função onScreenCall() para chamar
respondToCall()
e informar ao sistema como responder à nova chamada. Essa função usa um parâmetro
CallResponse
que pode ser usado para instruir o sistema a bloquear a chamada, rejeitá-la como se
o usuário tivesse feito isso ou silenciá-la. Também é possível instruir o sistema a não adicionar essa
chamada ao registro de chamadas do dispositivo.
// 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)
É necessário registrar a implementação de CallScreeningService no arquivo de manifesto
com o filtro de intent e a permissão adequados para que o sistema possa acioná-la
corretamente.
<service
android:name=".ScreeningService"
android:permission="android.permission.BIND_SCREENING_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallScreeningService" />
</intent-filter>
</service>