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

AndroidApps/ApplicationsMobile Development

This example demonstrates how do I send in android.

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:hint="Enter mobile number"
      android:id="@+id/editTextNum"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
   <EditText
      android:hint="Enter your message"
      android:id="@+id/editTextMsg"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
   <Button
      android:id="@+id/btnSendMsg"
      android:onClick="SendSMS"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Send Message"/>
</LinearLayout>

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

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
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;
public class MainActivity extends AppCompatActivity {
   Button btnSend;
   EditText editTextNum, editTextMsg;
   private static final int PERMISSION_REQUEST = 101;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      editTextNum = findViewById(R.id.editTextNum);
      editTextMsg = findViewById(R.id.editTextMsg);
      btnSend = findViewById(R.id.btnSendMsg);
   }
   public void SendSMS(View view) {
      int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS);
      if (permissionCheck == PackageManager.PERMISSION_GRANTED){
         MyMessage();
      } else {
         ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.SEND_SMS},
PERMISSION_REQUEST);
      }
   }
   private void MyMessage() {
      String myNumber = editTextNum.getText().toString().trim();
      String myMsg = editTextMsg.getText().toString().trim();
      if (myNumber.equals("") || myMsg.equals("")){
         Toast.makeText(this, "Field cannot be empty", Toast.LENGTH_SHORT).show();
      } else {
         if (TextUtils.isDigitsOnly(myNumber)){
            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
   public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      if (requestCode == PERMISSION_REQUEST) {
         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="app.com.sample">
   <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 the android studio, open one of your project's activity files and click the Run Play Icon icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

Click here to download the project code.

raja
Updated on 09-Jul-2020 06:18:59

Advertisements