There are two ways to send SMS in Android
- Using SmsManager
- Using Built in Intent
1. SmsManager
The SmsManager manages SMS operations such as sending data to the given mobile device. You can create this object by calling the static method SmsManager.getDefault() as follows:
Initialize
SmsManager smsManager = SmsManager.getDefault();
Sending Message
Once you have SmsManager object, you can use sendDataMessage() method to send SMS at the specified
mobile number as below:
smsManager.sendTextMessage("phoneNo", null, "SMS text", null, null);
API’s available
ArrayList
This method divides a message text into several fragments, none bigger than the maximum SMS message size.
static SmsManager getDefault()
This method is used to get the default instance of the SmsManager
void sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent)
This method is used to send a data based SMS to a specific application port.
void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList
Send a multi-part text based SMS.
void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
Send a text based SMS
2. Using Built-in Intent to send SMS
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
Intent Object – Data/Type to send SMS To send an SMS you need to specify smsto: as URI using setData() method and data type will be tovnd.android-dir/mms-sms using setType() method as follows:
smsIntent.setData(Uri.parse("smsto:")); smsIntent.setType("vnd.android-dir/mms-sms");
Add PhoneNumber
smsIntent.putExtra("address" , new String("0123456789;3393993300")); smsIntent.putExtra("sms_body" , "CoderzHeaven Sample Message");
Complete Code
Intent smsIntent = new Intent(Intent.ACTION_VIEW); smsIntent.setData(Uri.parse("smsto:")); smsIntent.setType("vnd.android-dir/mms-sms"); smsIntent.putExtra("address", new String("9876543210")); smsIntent.putExtra("sms_body", "CoderzHeaven Sample Message"); try { startActivity(smsIntent); finish(); Log.i("SMS Sent...", ""); } catch (android.content.ActivityNotFoundException ex) { Toast.makeText(this, "Sending message failed, please try again later.", Toast.LENGTH_SHORT).show(); }