CallRedirectionService Implementation not working

前端 未结 2 1726
孤独总比滥情好
孤独总比滥情好 2021-02-09 16:39

Android Q has introduced CallRedirectionService API - seems like one of the way 3rd party apps can use it is to cancel calls and reroute them over VoIP - essentiall

2条回答
  •  旧巷少年郎
    2021-02-09 17:16

    Got it working (looking here). The next code will block all outgoing calls (it's just a sample...) :

    MainActivity.kt

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            if (!isRedirection())
                roleAcquire(RoleManager.ROLE_CALL_REDIRECTION)
        }
    
        private fun isRedirection(): Boolean {
            return isRoleHeldByApp(RoleManager.ROLE_CALL_REDIRECTION)
        }
    
        private fun isRoleHeldByApp(roleName: String): Boolean {
            val roleManager: RoleManager?
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                roleManager = getSystemService(RoleManager::class.java)
                return roleManager.isRoleHeld(roleName)
            }
            return false
        }
    
        private fun roleAcquire(roleName: String) {
            val roleManager: RoleManager?
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                if (roleAvailable(roleName)) {
                    roleManager = getSystemService(RoleManager::class.java)
                    val intent = roleManager.createRequestRoleIntent(roleName)
                    startActivityForResult(intent, ROLE_ACQUIRE_REQUEST_CODE)
                } else {
                    Toast.makeText(this, "Redirection call with role in not available", Toast.LENGTH_SHORT).show()
                }
            }
        }
    
        private fun roleAvailable(roleName: String): Boolean {
            val roleManager: RoleManager?
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                roleManager = getSystemService(RoleManager::class.java)
                return roleManager.isRoleAvailable(roleName)
            }
            return false
        }
    
        companion object {
            private const val ROLE_ACQUIRE_REQUEST_CODE = 4378
        }
    }
    

    MyCallRedirectionService.kt

    class MyCallRedirectionService : CallRedirectionService() {
    
        override fun onPlaceCall(handle: Uri, initialPhoneAccount: PhoneAccountHandle, allowInteractiveResponse: Boolean) {
            Log.d("AppLog", "handle:$handle , initialPhoneAccount:$initialPhoneAccount , allowInteractiveResponse:$allowInteractiveResponse")
            cancelCall()
        }
    }
    

    AndroidManifest.xml

    
    
        
            
                
                    
    
                    
                
            
    
            
                
                    
                
            
        
    
    
    

提交回复
热议问题