कॉल स्क्रीन करें

Android 10 (एपीआई लेवल 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>