Home > database >  How do I implement a very basic broadcastreceiever with Kotlin?
How do I implement a very basic broadcastreceiever with Kotlin?

Time:04-05

I'm working on an app with a feature that can auto launch if the call state changes. Just for resting I went with a super basic implementation. This doesn't seem to work:

                   val receiver = object : BroadcastReceiver() {
                       override fun onReceive(context: Context, intent: Intent) {
                           Toast.makeText(context, "HELLO", Toast.LENGTH_SHORT).show()

                       }
                   }

                   registerReceiver(receiver, IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED))

I'm not getting my toast when I connect to a call. I'm pretty new to this and the docs for broadcastreceiver has me a bit confused so I've spent a few days trying to piece together examples and advice from reddit and stackoverflow.

CodePudding user response:

Make sure you have the right permissions, this event (https://developer.android.com/reference/android/telephony/TelephonyManager#ACTION_PHONE_STATE_CHANGED) requires the following permission:

Manifest.permission.READ_PHONE_STATE

  • Related