Skip to content

Instantly share code, notes, and snippets.

@TobiAlbert
Created June 20, 2020 04:03
Show Gist options
  • Save TobiAlbert/fb9cd53e6ebb231cb95553e1886cee40 to your computer and use it in GitHub Desktop.
Save TobiAlbert/fb9cd53e6ebb231cb95553e1886cee40 to your computer and use it in GitHub Desktop.
class SmsBroadcastReceiver : BroadcastReceiver() {
companion object {
const val TAG = "SmsBroadcastReceiver"
const val REQUEST_SMS_CONSENT = 1001
}
override fun onReceive(context: Context?, intent: Intent?) {
if (SmsRetriever.SMS_RETRIEVED_ACTION == intent?.action) {
val extras = intent.extras
val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status
when (smsRetrieverStatus.statusCode) {
CommonStatusCodes.SUCCESS -> {
val consentIntent = extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
try {
// Start Activity to show content dialog to user. Activity must be started
// within 5 minutes, otherwise you'll receive another TIMEOUT intent
context?.let { (it as AppCompatActivity).startActivityForResult(consentIntent!!, REQUEST_SMS_CONSENT) }
} catch (e: ActivityNotFoundException) {
Log.e(TAG, "No Activity Found: $e")
}
}
CommonStatusCodes.TIMEOUT -> {
// handle timeout
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment