block phone call in android studio kotlin

481 Views Asked by At

i am using firabse realtime database to store the list of numbers which has to be blocked . i also made a report actibity in which if a number is reported more then two times then the number will be added to blocked . i am not able to find a solution that how to block numbers .

val reportsRef = database.getReference("Reports")
    val blocklistRef = database.getReference("blocklist")

    // Create a reference to the blocklist node in the database


    // Listen for changes to the reports node
    reportsRef.addValueEventListener(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {

// Create a map to store the number of times each phone number appears in the reports node

            // Create a map to store the number of times each phone number appears in the reports node
            val phoneNumberCount = HashMap<String, Int>()

            // Iterate over all the reports and count the number of times each phone number appears
            for (report in snapshot.children) {
                val phoneNumber = report.child("reportednumber").value as String
                phoneNumberCount[phoneNumber] = (phoneNumberCount[phoneNumber] ?: 0) + 1
            }

            // Iterate over the phone numbers that have been reported more than two times and add them to the blocklist
            for ((phoneNumber, count) in phoneNumberCount) {
                if (count > 2) {
                    blocklistRef.child("+91$phoneNumber").setValue(true)
                }
            }
        }

this is the way i am getting the list of blocked number stored.i dont know much about broadcastreceiver can someone help me in blocking those numbers stored in realtime database

1

There are 1 best solutions below

2
mahdi tajdini On

To block phone numbers in Android, you can use the TelephonyManager class and the addOrUpdateBlockedNumber method. Here's an example of how you can use this method to block phone numbers stored in your Firebase Realtime Database:

    // Get a reference to the blocklist node in the database
    val blocklistRef = database.getReference("blocklist")

    // Listen for changes to the blocklist node
    blocklistRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(snapshot: DataSnapshot) {
        // Iterate over all the blocked numbers and add them to the system's 
    blocked numbers list
        for (blockedNumber in snapshot.children) {
            val phoneNumber = blockedNumber.key
            val isBlocked = blockedNumber.value as Boolean
            if (isBlocked) {
                val telephonyManager = 
    getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                telephonyManager.addOrUpdateBlockedNumber(phoneNumber)
            }
        }
    }

    override fun onCancelled(error: DatabaseError) {
        // Handle the error
    }
})

This code listens for changes to the blocklist node in your database, and when a new phone number is added with a value of true, it adds that number to the system's blocked numbers list using the TelephonyManager. Note that this code assumes that the phone numbers in your database are stored in the format +98XXXXXXXXXX . If your phone numbers are stored in a different format, you'll need to adjust the code accordingly.

Also, keep in mind that blocking phone numbers in this way only works on devices running Android 7.0 (API level 24) or higher. If your app needs to support older versions of Android, you'll need to use a different approach to block phone numbers.