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
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)
}