Hiện màn hình cuộc gọi

Các thiết bị chạy Android 10 (API cấp 29) trở lên cho phép ứng dụng của bạn xác định các lệnh gọi từ các số không có trong sổ địa chỉ của người dùng là các lệnh gọi có khả năng là cuộc gọi làm phiền. Người dùng có thể chọn tự động từ chối cuộc gọi làm phiền. Để mang lại thông tin rõ ràng hơn cho người dùng khi họ bỏ lỡ lệnh gọi, thông tin về các lệnh gọi bị chặn này sẽ được ghi lại trong nhật ký lệnh gọi. Việc sử dụng API Android 10 sẽ loại bỏ yêu cầu phải có được quyền READ_CALL_LOG của người dùng để cung cấp chức năng sàng lọc cuộc gọi và mã nhận dạng người gọi.

Bạn sẽ sử dụng phương thức triển khai CallScreeningService để sàng lọc lệnh gọi. Gọi hàm onScreenCall() cho mọi cuộc gọi mới hoặc cuộc gọi đi mới khi số đó không có trong danh bạ của người dùng. Bạn có thể kiểm tra đối tượng Call.Details để biết thông tin về lệnh gọi. Cụ thể, hàm getCallerNumberVerificationStatus() bao gồm thông tin từ nhà cung cấp mạng về số khác. Nếu trạng thái xác minh không thành công, thì đây là một dấu hiệu tốt cho thấy cuộc gọi đến từ một số không hợp lệ hoặc cuộc gọi có khả năng là cuộc gọi làm phiền.

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

Đặt hàm onScreenCall() để gọi respondToCall() nhằm cho hệ thống biết cách phản hồi lệnh gọi mới. Hàm này lấy một tham số CallResponse mà bạn có thể dùng để yêu cầu hệ thống chặn, từ chối lệnh gọi như thể người dùng đã làm hoặc tắt tiếng. Bạn cũng có thể yêu cầu hệ thống bỏ qua việc thêm lệnh gọi này vào nhật ký cuộc gọi của thiết bị.

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

Bạn phải đăng ký phương thức triển khai CallScreeningService trong tệp kê khai bằng bộ lọc ý định và quyền thích hợp để hệ thống có thể kích hoạt chính xác phương thức đó.

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