There is different ways to send SMS in Android:
1. By redirecting to messaging app
2. Auto SMS using SmsManager without going to messaging app
1. Through messaging app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:9876543210"));
intent.putExtra("sms_body", "Hello, How are you?");
startActivity(intent);
Above code will redirect you to the Messaging app (or if you have multiple messaging app then it will give options to select) with pre-populated sender number and message body.
Note: Above code don’t require SMS permission
2. Through SMSManager
public final class SmsManager
extends Object
java.lang.Object
-> android.telephony.SmsManager
Added in API level 4
SMSManager manages SMS operations such as sending data, text, and pdu SMS messages.
You can get SMSManager object by calling below methods:
1. By static method getDefault()
–> Which will send the SMS through default provider
2. By static method getSmsManagerForSubscriptionId(int)
–> Which will send the SMS through particular sim, which Subscription id is passed as parameter. For getting subscription id of a sim, we have to use SubscriptionManager.
Note: For sending SMS, we need SEND_SMS permission
SubscriptionManager
Added in API level 22
public class SubscriptionManager
extends Object
java.lang.Object
-> android.telephony.SubscriptionManager
SubscriptionManager is the application interface to SubscriptionController and provides information about the current Telephony Subscriptions.
Note: For getting active subscription info list, we need READ_PHONE_STATE permission
Below is an example to get the sim provider info list and set the sim carrier name as button text, So user can choose a particular carrier from which he/she want to send the SMS.
In AndroidManifest.xml, Add below permissions:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
MainActivity class:
import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String SENT_SMS_ACTION = "SENT_SMS_ACTION";
private static final String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
HashMap<Integer, HashMap<String, Object>> carriersInfo = new LinkedHashMap<>();
Button sim1, sim2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_PHONE_STATE}, 1001);
}
registerReceiver(sendReceiver, new IntentFilter(SENT_SMS_ACTION));
registerReceiver(deliveredReceiver, new IntentFilter(DELIVERED_SMS_ACTION));
sim1 = findViewById(R.id.sendSMS1);
sim1.setOnClickListener(this);
sim2 = findViewById(R.id.sendSMS2);
sim2.setOnClickListener(this);
findViewById(R.id.sub_list).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.sendSMS1:
int subId = (int) carriersInfo.get(0).get("subscriptionId");
sendSMS(subId);
break;
case R.id.sendSMS2:
int subId2 = (int) carriersInfo.get(1).get("subscriptionId");
sendSMS(subId2);
break;
case R.id.sub_list:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return;
}
}
List<SubscriptionInfo> subscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
int id = 0;
if (subscriptionInfoList != null && subscriptionInfoList.size() > 0) {
for (SubscriptionInfo subscriptionInfo : subscriptionInfoList) {
int subscriptionId = subscriptionInfo.getSubscriptionId();
String number = subscriptionInfo.getNumber();
String carrierName = (String) subscriptionInfo.getCarrierName();
HashMap<String, Object> carrierInfo = new LinkedHashMap<>();
carrierInfo.put("subscriptionId", subscriptionId);
carrierInfo.put("carrierName", carrierName);
carrierInfo.put("number", number);
carriersInfo.put(id, carrierInfo);
if (id == 0) {
sim1.setText(carrierName);
} else {
sim2.setText(carrierName);
}
id++;
}
}
}
break;
}
}
private void sendSMS(int subId) {
SmsManager smsManager = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
} else {
smsManager = SmsManager.getDefault();
}
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(MainActivity.this, 101, sentIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent deliveredIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliveredPI = PendingIntent.getBroadcast(MainActivity.this, 102, deliveredIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//TODO: Update destination mobile number and message
smsManager.sendTextMessage("+919876543210", null, "Hello", sentPI, deliveredPI);
}
private BroadcastReceiver sendReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (getResultCode() == Activity.RESULT_OK) {
Toast.makeText(MainActivity.this, "SMS Send Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Something went wrong. Please try again.", Toast.LENGTH_SHORT).show();
}
}
};
private BroadcastReceiver deliveredReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (getResultCode() == Activity.RESULT_OK) {
Toast.makeText(MainActivity.this, "SMS delivered Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Something went wrong on delivery. Please try again.", Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(sendReceiver);
unregisterReceiver(deliveredReceiver);
}
}
In above code first we get the sim provider list and set the carrier name as button text. So user will get to choose from which carrier he/she want to send the SMS. After selecting carrier, we will trigger the SMS through SMSManager which will also give the status of SMS sending and delivering.
References:
https://developer.android.com/reference/android/telephony/SmsManager
https://stackoverflow.com/questions/27351936/how-to-send-a-sms-using-smsmanager-in-dual-sim-mobile
https://developer.android.com/reference/android/telephony/SubscriptionManager