How to send a SMS using SMSmanager in Dual SIM mobile in Android using Kotlin?


This example demonstrates how to send a SMS using SMSmanager in Dual SIM mobile in Android using Kotlin.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="4dp"
   tools:context=".MainActivity">
   <EditText
      android:id="@+id/editTextNum"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Enter mobile number"
      android:textColor="@android:color/black" />
   <EditText
      android:id="@+id/editTextMsg"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Enter your message"
      android:textColor="@android:color/black" />
   <Button
      android:id="@+id/btnSendMsg"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:layout_marginTop="5dp"
      android:onClick="sendMessage"
      android:text="Send Message" />
</LinearLayout>

Step 3 − Add the following code to src/MainActivity.kt

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.telephony.SmsManager
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
   lateinit var button: Button
   lateinit var editTextNumber: EditText
   lateinit var editTextMessage: EditText
   private val permissionRequest = 101
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      editTextNumber = findViewById(R.id.editTextNum)
      editTextMessage = findViewById(R.id.editTextMsg)
      button = findViewById(R.id.btnSendMsg)
   }
   fun sendMessage(view: View) {
      val permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
      if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
         myMessage()
      } else {
         ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.SEND_SMS),
         permissionRequest)
      }
   }
   private fun myMessage() {
      val myNumber: String = editTextNumber.text.toString().trim()
      val myMsg: String = editTextMessage.text.toString().trim()
      if (myNumber == "" || myMsg == "") {
         Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show()
      } else {
         if (TextUtils.isDigitsOnly(myNumber)) {
            val smsManager: SmsManager = SmsManager.getDefault()
            smsManager.sendTextMessage(myNumber, null, myMsg, null, null)
            Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show()
         } else {
            Toast.makeText(this, "Please enter the correct number", Toast.LENGTH_SHORT).show()
         }
      }
   }
   override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults:
   IntArray) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults)
      if (requestCode == permissionRequest) {
         if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            myMessage();
         } else {
            Toast.makeText(this, "You don't have required permission to send a message",
            Toast.LENGTH_SHORT).show();
         }
      }
   }
}

Step 4 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.q11">
   <uses-permission android:name="android.permission.SEND_SMS" />
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen


Updated on: 05-Nov-2020

689 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements