Akshay Saambram
Akshay Saambram

Reputation: 350

get Incoming call contact name and phone number using android studio kotlin

I'm trying to construct an app that requires the name and phone number of the incoming call contact.

I did research to find a solution, but all the data I found was in java and there wasn't much in kotlin.

Code found in Java :

public class ServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, Intent intent) {
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                super.onCallStateChanged(state, incomingNumber);
                System.out.println("incomingNumber : "+incomingNumber);
            }
        },PhoneStateListener.LISTEN_CALL_STATE);
    }
}

Converted above code to kotlin :

@Suppress("DEPRECATION", "DEPRECATION")
class ServiceReceiver : BroadcastReceiver() {
    @SuppressLint("UnsafeProtectedBroadcastReceiver", "InlinedApi")
    override fun onReceive(context : Context?, intent : Intent?) {
        val tm = context?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            tm.registerTelephonyCallback(
                context.mainExecutor,
                object : TelephonyCallback(), TelephonyCallback.CallStateListener {
                    override fun onCallStateChanged(state: Int) {
                    }
                })
        } else {
            tm.listen(object : PhoneStateListener() {
                @Deprecated("Deprecated in Java")
                override fun onCallStateChanged(state: Int, incomingNumber: String) {
                    super.onCallStateChanged(state, incomingNumber)
                    println("incomingNumber : $incomingNumber")
                }
            }, PhoneStateListener.LISTEN_CALL_STATE)
        }
    }
}

I attempted to implement the above code in kotlin, my println function got executed but was like below lines!

D/CompatibilityChangeReporter: Compat change id reported: 147600208; UID 10122; state: ENABLED
I/System.out: incomingNumber :  

I decided to put it here, so if you could please assist.

Upvotes: 1

Views: 2269

Answers (1)

Abdullah Javed
Abdullah Javed

Reputation: 734

Here is the kotlin version of this class.

    class ServiceReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val telephony = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            telephony.registerTelephonyCallback(
                context.mainExecutor,
                object : TelephonyCallback(), TelephonyCallback.CallStateListener {
                    override fun onCallStateChanged(state: Int) {
                    }
                })
        } else {
            telephony.listen(object : PhoneStateListener() {
                override fun onCallStateChanged(state: Int, incomingNumber: String) {
                    super.onCallStateChanged(state, incomingNumber)
                    println("incomingNumber : $incomingNumber")
                }
            }, PhoneStateListener.LISTEN_CALL_STATE)
        }
    }
}

Upvotes: 1

Related Questions