कॉल को रीडायरेक्ट करना

Android 10 या इसके बाद के वर्शन वाले डिवाइस, कॉल इंटेंट को इससे अलग तरीके से मैनेज करते हैं जो Android 9 या इससे पहले के वर्शन पर काम करते हैं. Android 10 और उसके बाद वाले वर्शन पर, ACTION_NEW_OUTGOING_CALL ब्रॉडकास्ट पर रोक लगा दी गई है और उसे इससे बदल दिया गया है CallRedirectionService एपीआई. CallRedirectionService आपको इंटरफ़ेस उपलब्ध कराता है, ताकि आप इनका इस्तेमाल कर सकें Android प्लैटफ़ॉर्म से किए जाने वाले आउटगोइंग कॉल में बदलाव कर सकता है. उदाहरण के लिए, तीसरा-पक्ष ऐप्लिकेशन, कॉल को रद्द कर सकते हैं और उन्हें VoIP पर रूट कर सकते हैं.

Kotlin

class RedirectionService : CallRedirectionService() {
    override fun onPlaceCall(
        handle: Uri,
        initialPhoneAccount: PhoneAccountHandle,
        allowInteractiveResponse: Boolean
    ) {
        // Determine if the call should proceed, be redirected, or cancelled.
        val callShouldProceed = true
        val callShouldRedirect = false
        when {
            callShouldProceed -> {
                placeCallUnmodified()
            }
            callShouldRedirect -> {
                // Update the URI to point to a different phone number or modify the
                // PhoneAccountHandle and redirect.
                redirectCall(handle, initialPhoneAccount, true)
            }
            else -> {
                cancelCall()
            }
        }
    }
}

Java

class RedirectionService extends CallRedirectionService {
    @Override
    public void onPlaceCall(
            @NonNull Uri handle,
            @NonNull PhoneAccountHandle initialPhoneAccount,
            boolean allowInteractiveResponse
    ) {
        // Determine if the call should proceed, be redirected, or cancelled.
        // Your app should implement this logic to determine the redirection.
        boolean callShouldProceed = true;
        boolean callShouldRedirect = false;
        if (callShouldProceed) {
            placeCallUnmodified();
        } else if (callShouldRedirect) {
            // Update the URI to point to a different phone number or modify the
            // PhoneAccountHandle and redirect.
            redirectCall(handle, initialPhoneAccount, true);
        } else {
            cancelCall();
        }
    }
}

आपको इस सेवा को मेनिफ़ेस्ट में रजिस्टर करना होगा, ताकि सिस्टम इसे चालू कर सके सही तरीके से.

<service
    android:name=".RedirectionService"
    android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">
    <intent-filter>
        <action android:name="android.telecom.CallRedirectionService"/>
    </intent-filter>
</service>

रीडायरेक्शन सेवा का इस्तेमाल करने के लिए, आपके ऐप्लिकेशन को कॉल रीडायरेक्शन भूमिका के लिए अनुरोध करना होगा RoleManager से. यह पूछेगा उस उपयोगकर्ता को जोड़ें, जो आपके ऐप्लिकेशन को कॉल रीडायरेक्ट मैनेज करने की अनुमति देना चाहता हो. अगर आपका ऐप्लिकेशन को यह भूमिका नहीं दी गई है, इसलिए रीडायरेक्ट करने की आपकी सेवा का इस्तेमाल नहीं किया जाएगा.

आपको यह देखना चाहिए कि जब उपयोगकर्ता आपका ऐप्लिकेशन लॉन्च करता है, तब आपके ऐप्लिकेशन के पास यह भूमिका है या नहीं. तो ज़रूरत के हिसाब से अनुरोध किया जा सकता है. RoleManager के बनाए गए किसी इंटेंट को लॉन्च किया जाता है, इसलिए सुनिश्चित करें कि आप onActivityResult() का इस्तेमाल करता है.

Kotlin

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Tell the system that you want your app to handle call redirects. This
        // is done by using the RoleManager to register your app to handle redirects.
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            val roleManager = getSystemService(Context.ROLE_SERVICE) as RoleManager
            // Check if the app needs to register call redirection role.
            val shouldRequestRole = roleManager.isRoleAvailable(RoleManager.ROLE_CALL_REDIRECTION) &&
                    !roleManager.isRoleHeld(RoleManager.ROLE_CALL_REDIRECTION)
            if (shouldRequestRole) {
                val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION)
                startActivityForResult(intent, REDIRECT_ROLE_REQUEST_CODE)
            }
        }
    }

    companion object {
        private const val REDIRECT_ROLE_REQUEST_CODE = 1
    }
}

Java

class MainActivity extends AppCompatActivity {
    private static final int REDIRECT_ROLE_REQUEST_CODE = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Tell the system that you want your app to handle call redirects. This
        // is done by using the RoleManager to register your app to handle redirects.
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            RoleManager roleManager = (RoleManager) getSystemService(Context.ROLE_SERVICE);
            // Check if the app needs to register call redirection role.
            boolean shouldRequestRole = roleManager.isRoleAvailable(RoleManager.ROLE_CALL_REDIRECTION) &&
                    !roleManager.isRoleHeld(RoleManager.ROLE_CALL_REDIRECTION);
            if (shouldRequestRole) {
                Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION);
                startActivityForResult(intent, REDIRECT_ROLE_REQUEST_CODE);
            }
        }
    }
}