Ask Your Question
1

How can I be notified of incoming calls in the background using Flutter and Kotlin?

asked 2021-07-27 11:00:00 +0000

devzero gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-25 04:00:00 +0000

djk gravatar image

To be notified of incoming calls in the background using Flutter and Kotlin, you can follow the following steps:

  1. Create a Flutter project and add Kotlin support to it.
  2. Create a Kotlin class that extends BroadcastReceiver.
  3. Override the onReceive() function of the BroadcastReceiver class.
  4. Register the BroadcastReceiver class in the AndroidManifest.xml file.
  5. Create a Flutter plugin that calls the Kotlin BroadcastReceiver class.

Here is the sample code for the Kotlin BroadcastReceiver class:

class CallReceiver: BroadcastReceiver() {
    private var callStateListener: PhoneStateListener? = null
    private lateinit var telephonyManager: TelephonyManager

    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent?.action == TelephonyManager.ACTION_PHONE_STATE_CHANGED) {
            val state = intent.getStringExtra(TelephonyManager.EXTRA_STATE)
            when (state) {
                TelephonyManager.EXTRA_STATE_RINGING -> {
                    val incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER)
                    // Do something with the incoming number
                }
            }
        }
    }

    fun startListening(context: Context) {
        callStateListener = object : PhoneStateListener() {
            override fun onCallStateChanged(state: Int, phoneNumber: String?) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    // Do something with the incoming number
                }
            }
        }
        telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE)
    }

    fun stopListening() {
        telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_NONE)
    }
}

In your Flutter plugin, you can create methods that call the startListening() and stopListening() functions of the CallReceiver class to start and stop listening for incoming calls. You can use MethodChannels to communicate between Flutter and the native platform.

With this approach, you can receive notifications of incoming calls even when your app is in the background.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-07-27 11:00:00 +0000

Seen: 12 times

Last updated: Jan 25 '22