1
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
    phnNbr = intent?.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER).toString()
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
    val telephony =
        context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    telephony.listen(object : PhoneStateListener() {
        override fun onCallStateChanged(state: Int, incomingNumber: String) {
//                        super.onCallStateChanged(state, incomingNumber)
            println("incomingNumber API>28: $incomingNumber")
            phnNbr = incomingNumber
        }
    }, PhoneStateListener.LISTEN_CALL_STATE)
}

i am getting blank string using this code in API 30

I/System.out: incomingNumber API>28:

also the onCallStateChanged has been depreated for android 12, and the TelephonyCallback cannot provide the phone number. i also would like to know a workaround for that.

Rauson Ali
  • 111
  • 10
  • https://stackoverflow.com/questions/69571012/telephonymanager-deprecated-listen-call-state-ringing-on-android-12 – A Farmanbar Dec 14 '21 at 13:27
  • Actually i need the Phone Number. I tested my code further and found out this gives the number only if the app is open. asked another question here https://stackoverflow.com/questions/70351893/telephony-listen-phonestatelistener-not-working-when-app-is-closed Thanks for the suggestion though – Rauson Ali Dec 14 '21 at 16:19

0 Answers0