Trying to disconnect an incoming call after few seconds of ringing on Android 10. Using CallScreeningService for filtering an incoming call.
@RequiresApi(Build.VERSION_CODES.N)
class MyCallScreeningService :
CallScreeningService() {
@RequiresApi(Build.VERSION_CODES.Q)
override fun onScreenCall(details: Call.Details?) {
if (details?.callDirection == Call.Details.DIRECTION_INCOMING) {
val phoneNumber = details.handle.schemeSpecificPart
val blockedList = Paper.book().read(Constants.FIELD_BLOCKED_LIST, ArrayList<String>())
if (blockedList.contains(phoneNumber)) {
disconnectCall(details, phoneNumber)
}
}
}
private fun disconnectCall(details: Call.Details, phoneNumber: String) {
Handler().postDelayed({
respondToCall(details, buildResponse())
}, 3000L)
}
private fun buildResponse(): CallResponse {
return CallResponse.Builder()
.setRejectCall(true)
.setDisallowCall(true)
.setSkipNotification(true)
.setSkipCallLog(true)
.build()
}
}
Problem is, if I set the handler time too low, the call gets disconnected without ringing on receiver's side. Although, if I set it higher like 6 secs, then the ringing notification shows, but disconnection doesn't work.