สกรีนสายเรียกเข้า

อุปกรณ์ที่ใช้ 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 ในไฟล์ Manifest ด้วยตัวกรอง Intent และสิทธิ์ที่เหมาะสมเพื่อให้ระบบเรียกใช้ได้อย่างถูกต้อง

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