過濾電話

如果裝置搭載 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
            }
        }
    }
}

設定 onScreenCall() 函式以呼叫 respondToCall(),告訴系統如何回應新的呼叫。這個函式可接受 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>