Created
September 29, 2024 10:48
-
-
Save BraveEvidence/91437e7e0fb1da56f51259a52f762922 to your computer and use it in GitHub Desktop.
SmsRretriverGist MySMSBroadcastReceiver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.BroadcastReceiver | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Build | |
import android.widget.Toast | |
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 MySMSBroadcastReceiver: BroadcastReceiver() { | |
override fun onReceive(context: Context?, intent: Intent?) { | |
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent?.action) { | |
val extras = intent.extras | |
val smsRetrieverStatus = | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | |
extras?.getParcelable(SmsRetriever.EXTRA_STATUS, Status::class.java) | |
} else { | |
@Suppress("DEPRECATION") | |
extras?.get(SmsRetriever.EXTRA_STATUS) as? Status | |
} | |
when (smsRetrieverStatus?.statusCode) { | |
CommonStatusCodes.SUCCESS -> { | |
val message = | |
extras?.getString(SmsRetriever.EXTRA_SMS_MESSAGE) | |
message?.let { | |
SmsLiveDataHolder.postSmsMessage(it) | |
} | |
} | |
CommonStatusCodes.TIMEOUT -> { | |
Toast.makeText(context, "Timeout error", Toast.LENGTH_LONG).show() | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full tutorial here https://www.youtube.com/watch?v=-YxaNRPd5aU