コール スクリーニング

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

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

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

respondToCall() を呼び出すように onScreenCall() 関数を設定し、新しい呼び出しへの応答方法をシステムに指示します。この関数は CallResponse パラメータを受け取ります。これを使用して、通話をブロックするか、ユーザーが発信した場合と同様に拒否するか、サイレント モードにするようシステムに指示できます。また、この呼び出しをデバイスの通話履歴に追加しないようシステムに指示することもできます。

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

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

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