コール スクリーニング

Android 10(API レベル 29)以降を搭載したデバイスでは、ユーザーのアドレス帳に登録されていない番号からの通話を、迷惑電話の可能性があるものとして識別できます。迷惑電話をサイレントで拒否するよう設定できます。不在着信があったときにユーザーに透明性を提供するため、これらのブロックされた通話に関する情報は通話履歴に記録されます。Android 10 API を使用すると、通話スクリーニング機能と発信者番号通知機能を提供するために、ユーザーから READ_CALL_LOG 権限を取得する必要がなくなります。

CallScreeningService の実装を使用して通話をスクリーニングします。番号がユーザーの連絡先リストにない場合、新しい着信または発信に対して onScreenCall() 関数を呼び出します。Call.Details オブジェクトで、通話に関する情報を確認できます。具体的には、getCallerNumberVerificationStatus() 関数には、ネットワーク プロバイダから取得したもう一方の番号に関する情報が含まれます。確認ステータスが失敗した場合は、通話が無効な番号からのものであるか、迷惑電話の可能性があることを示しています。

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.
                }
            }
        }
    }
}

onScreenCall() 関数を設定して respondToCall() を呼び出し、新しい通話にどのように応答するかをシステムに指示します。この関数は CallResponse パラメータを受け取ります。このパラメータを使用して、通話をブロックする、ユーザーが拒否したかのように拒否する、または消音するようシステムに指示できます。また、この通話をデバイスの通話履歴に追加しないようにシステムに指示することもできます。

// 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)

システムが CallScreeningService の実装を正しくトリガーできるように、適切なインテント フィルタと権限を使用してマニフェスト ファイルに登録する必要があります。

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