📄Broadcast Directory

Broadcast Directory contains code to respond to system-wide events for Matar app on android device.


Broadcast Directory

--broadcast(directory)
  --SmsBroadcastReceiver.kt(file)()

SmsBroadcastReceiver.kt

package com.matar.app.broadcast

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.google.android.gms.auth.api.phone.SmsRetriever
import com.google.android.gms.common.api.CommonStatusCodes
import com.google.android.gms.common.api.Status

class SmsBroadcastReceiver : BroadcastReceiver() {
    var smsBroadcastReceiverListener: SmsBroadcastReceiverListener? = null

    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent?.action == SmsRetriever.SMS_RETRIEVED_ACTION) {
            val extras = intent.extras
            val smsRetrieverStatus: Status = extras?.get(SmsRetriever.EXTRA_STATUS) as Status
            when (smsRetrieverStatus.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val messageIntent: Intent? =
                        extras.getParcelable(SmsRetriever.EXTRA_CONSENT_INTENT)
                    smsBroadcastReceiverListener?.onSuccess(messageIntent!!)
                }
                CommonStatusCodes.TIMEOUT -> {
                    smsBroadcastReceiverListener!!.onFailure()
                }
            }
        }
    }

    interface SmsBroadcastReceiverListener {
        fun onSuccess(intent: Intent)
        fun onFailure()
    }
}

Describing Code:

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events. Broadcast Receivers allow us to register for the system and application events, and when that event happens, then the register receivers get notified.

To receive SMS messages, ovveride the onReceive() method of the BroadcastReceiver class. The Android framework sends out system broadcasts of events such as receiving an SMS message, containing intents that are meant to be received using a BroadcastReceiver. You need to add the RECEIVE_SMS permission to your app's AndroidManifest.xml file.

.Here's a breakdown of what the code does:

  1. The class SmsBroadcastReceiver extends BroadcastReceiver, which is a component that allows your app to receive and react to broadcast messages sent by the Android system or other apps.

  2. The smsBroadcastReceiverListener property is declared, which is an instance of the SmsBroadcastReceiverListener interface. This interface defines two callback methods: onSuccess and onFailure, which are used to handle the results of SMS retrieval.

  3. In the onReceive method, the code checks if the received broadcast intent has the action SmsRetriever.SMS_RETRIEVED_ACTION. This action is typically sent by the SmsRetriever API when a new SMS message is received.

  4. If the action matches, the code extracts additional data from the intent's extras. It retrieves the smsRetrieverStatus from the extras, which is of type Status, and checks its statusCode.

  5. If the statusCode is CommonStatusCodes.SUCCESS, it means that the SMS retrieval was successful. The code then attempts to retrieve a messageIntent from the extras and calls the onSuccess callback on the smsBroadcastReceiverListener if it exists.

  6. If the statusCode is CommonStatusCodes.TIMEOUT, it means that the SMS retrieval process timed out, and the code calls the onFailure callback on the smsBroadcastReceiverListener.

  7. Finally, the SmsBroadcastReceiverListener interface is defined with two callback methods: onSuccess, which takes an Intent as a parameter and is called when SMS retrieval is successful, and onFailure, which is called when SMS retrieval fails.

This code is useful for handling SMS-based authentication or verification in your Android app, where you can use the onSuccess callback to process the received SMS message or the onFailure callback to handle errors or timeouts. Remember to set the smsBroadcastReceiverListener property with an appropriate listener in your app's code to receive the callbacks when SMS messages are received or when there are errors.

Last updated