1

I work with Kotlin Android Studio

Imagine this scenario.

I have an application that according to the conditions of the users, I want them to be able to access their information without access to the Internet and by using SMS.

For this, it is necessary that the application can access the content of received SMS and analyze them (receive SMS from a specific number and analyze the text of the message)

There is also a need for the application to be able to send a defined message to a specific number by accessing the user's SMS and waiting for a response.

I have watched various videos on YouTube and also read on various sites about how to receive and send SMS, but none of them worked.

I want you to give me a sample code for receiving and sending SMS

Amir Elahi
  • 31
  • 5

1 Answers1

3

First you need to request some permissions, put this in your Manifest.xml file:

<uses-feature
        android:name="android.hardware.telephony"
        android:required="true" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />

Than you can use this code directly in your Fragment to receive and send SMSs:

//This code to check if permissions are granted (used in fragment) than request them if not
if (activity?.checkSelfPermission(android.Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED
|| activity?.checkSelfPermission(android.Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED
|| activity?.checkSelfPermission(android.Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED
) {
    activity?.requestPermissions(
        arrayOf(android.Manifest.permission.RECEIVE_SMS,
            android.Manifest.permission.SEND_SMS,
            android.Manifest.permission.READ_SMS), PackageManager.PERMISSION_GRANTED
    )
}

//And this code to wait for and recive SMS
val br = object : BroadcastReceiver() {
    override fun onReceive(p0: Context?, p1: Intent?) {
        for (sms in Telephony.Sms.Intents.getMessagesFromIntent(
            p1
        )) {
            val smsSender = sms.originatingAddress
            val smsMessageBody = sms.displayMessageBody
            if (smsSender == "the_number_that_you_expect_the_SMS_to_come_FROM") {
                binding.textView.text = smsMessageBody
                break
            }
        }
    }
}

//register this broadcast receiver here
registerReceiver(
requireContext(),
br,
IntentFilter("android.provider.Telephony.SMS_RECEIVED"),
RECEIVER_EXPORTED
)

//This function for sending SMS
fun sendSms(phoneNumber: String, message: String) {
    val smsManager = SmsManager.getDefault()
    smsManager.sendTextMessage(phoneNumber, null, message, null, null)
}

Or you can use it in your Activity like this:

if (checkSelfPermission(android.Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(android.Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(android.Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED
) {
    requestPermissions(
        arrayOf(android.Manifest.permission.RECEIVE_SMS,
            android.Manifest.permission.SEND_SMS,
            android.Manifest.permission.READ_SMS), PackageManager.PERMISSION_GRANTED
    )
}


val br = object : BroadcastReceiver() {
    override fun onReceive(p0: Context?, p1: Intent?) {
        for (sms in Telephony.Sms.Intents.getMessagesFromIntent(
            p1
        )) {
            val smsSender = sms.originatingAddress
            val smsMessageBody = sms.displayMessageBody
            if (smsSender == "the_number_that_you_expect_the_SMS_to_come_FROM") {
                binding.textView.text = smsMessageBody
                break
            }
        }
    }
}

registerReceiver(
br,
IntentFilter("android.provider.Telephony.SMS_RECEIVED"),
RECEIVER_EXPORTED
)

//This function for sending SMS
fun sendSms(phoneNumber: String, message: String) {
    val smsManager = SmsManager.getDefault()
    smsManager.sendTextMessage(phoneNumber, null, message, null, null)
}
Jamal N
  • 351
  • 1
  • 6
  • What is "activity"? it is unresolved reference – Amir Elahi Aug 14 '23 at 10:28
  • It is a function (getActivivty) that returns the activity that the fragment is currently associated with, you can use requireActivity() instead. However, if you are using this code in an activity you can drop the(activity) function. – Jamal N Aug 14 '23 at 13:24
  • I am a little confused Please add the relevant code to your answer – Amir Elahi Aug 14 '23 at 17:42
  • I have added the code for activities – Jamal N Aug 14 '23 at 17:53
  • And what is "binding"? – Amir Elahi Aug 14 '23 at 18:08
  • And related to `SmsManager.getDefault()` I should say that A line is drawn on `getDefault()` and android studio says `getDefault() : SmsManager` **is deprecated** – Amir Elahi Aug 14 '23 at 18:22
  • Your answer will be appreciated @Jamal-D – Amir Elahi Aug 15 '23 at 17:00
  • binding is a way for accessing your xml views, you can either use binding, or findViewById. if you don't know what findViewById is than you really need to revisit the basic of Android development. Regarding smsManager.getDefault() , it's weird that you are getting deprecated warning, you can find a way to solve this here https://stackoverflow.com/questions/26921774/how-to-avoid-deprecation-warnings-when-suppresswarningsdeprecation-doesnt – Jamal N Aug 15 '23 at 17:28
  • my problem is "binding" does refer to nothing It has no definition in the codes above – Amir Elahi Aug 15 '23 at 18:13
  • I want you to define what is "binding" in code please – Amir Elahi Aug 16 '23 at 15:13
  • @AmirElahi , [viewBinding](https://developer.android.com/topic/libraries/view-binding) – anatoli Aug 29 '23 at 10:05