• telephonyManager.registerTelephonyCallback problem android 31 and above

    From Jakub@21:1/5 to All on Sat Sep 7 09:39:02 2024
    I have this variable at mainActiity

    private lateinit var telephonyManager: TelephonyManager


    and this fun

    fun initTelephonyManager06() {
    Log.i("ppp", "fun initTelephonyManager06() {")

    telephonyManager =
    getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

    if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ) {
    Log.i("ppp", "if (Build.VERSION.SDK_INT >=
    Build.VERSION_CODES.S) {")
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
    == PackageManager.PERMISSION_GRANTED ) {

    telephonyManager.registerTelephonyCallback(
    mainExecutor,
    object : TelephonyCallback(), TelephonyCallback.CallStateListener {
    override fun onCallStateChanged(state: Int) {
    Log.i("ppp", "override fun onCallStateChanged(state: Int) {")
    // Handle call state change
    when (state) {
    TelephonyManager.CALL_STATE_IDLE -> {
    Log.i("ppp", "State changed: " +
    state + " Idle")
    }

    TelephonyManager.CALL_STATE_OFFHOOK -> {
    Log.i("ppp", "State changed: " +
    state + " Offhook")
    }

    TelephonyManager.CALL_STATE_RINGING -> {
    Log.i("ppp", "State changed: " +
    state + " Ringing")
    }

    else -> {}
    }
    }
    })
    }
    } else {
    Log.i("ppp", "if (Build.VERSION.SDK_INT <
    Build.VERSION_CODES.S) {")
    telephonyManager.listen(object : PhoneStateListener() {
    override fun onCallStateChanged(state: Int,
    phoneNumber: String?) {
    // Handle call state change
    when ( state ) {
    TelephonyManager.CALL_STATE_IDLE -> {
    Log.i("ppp", "State changed: " + state + "
    Idle")
    }

    TelephonyManager.CALL_STATE_OFFHOOK -> {
    Log.i("ppp", "State changed: " + state + " Offhook")
    }

    TelephonyManager.CALL_STATE_RINGING -> {
    Log.i("ppp","State changed: " + state + " Ringing")
    }

    else -> {}
    }
    }
    }, PhoneStateListener.LISTEN_CALL_STATE)
    }

    }



    and in androidmanifest.xml this permision

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    under android 31 S it work ok. Show every state.

    But android upper or equal 31 don't show the state.

    How to fix it? What to do?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)