전화 리디렉션

Android 10 이상을 실행하는 기기는 Android 9 이하를 실행하는 기기와 다르게 통화 인텐트를 관리합니다. Android 10 이상에서는 ACTION_NEW_OUTGOING_CALL 브로드캐스트가 지원 중단되고 CallRedirectionService API로 대체됩니다. CallRedirectionService는 Android 플랫폼에서 발신 전화를 수정하는 데 사용할 수 있는 인터페이스를 제공합니다. 예를 들어 서드 파티 앱은 통화를 취소하고 VoIP를 통해 다시 라우팅할 수 있습니다.

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

시스템이 서비스를 올바르게 시작할 수 있도록 매니페스트에 이 서비스를 등록해야 합니다.

<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() 함수를 재정의하여 사용자의 선택을 처리해야 합니다.

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

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