Android Send SMS with Examples

  Total Views : 30
  Comments : 0
  Keywords : Android send sms using smsmanager api with examples, Android send sms using built-in sms application with examples

Here we will learn send sms in android with examples, how to use android smsmanager api to send sms with examples and how to send sms using intents in android by invoking default sms app with examples.

Android Send SMS

In android, we can send SMS from our android application in two ways either by using SMSManager api or Intents based on our requirements.

 

If we use SMSManager api, it will directly send SMS from our application. In case if we use Intent with proper action (ACTION_VIEW), it will invoke built-in SMS app to send SMS from our application.

Android Send SMS using SMSManager API

In android, to send SMS using SMSManager API we need to write the code like as shown below.

 

SmsManager smgr = SmsManager.getDefault();
smgr.sendTextMessage(
MobileNumber,null,Message,null,null);

SMSManager API required SEND_SMS permission in our android manifest to send SMS. Following is the code snippet to set SEND_SMS permissions in manifest file.

 

<uses-permission android:name="android.permission.SEND_SMS"/>

Android Send SMS using Intent

In android, Intent is a messaging object which is used to request an action from another app component such as activitiesservicesbroadcast receivers and content providers. To know more about Intent object in android check this Android Intents with Examples.

 

To send SMS using Intent object, we need to write the code like as shown below.

 

Intent sInt = new Intent(Intent.ACTION_VIEW);
sInt.putExtra(
"address", new String[]{txtMobile.getText().toString()});
sInt.putExtra(
"sms_body",txtMessage.getText().toString());
sInt.setType(
"vnd.android-dir/mms-sms");

Even for Intent, it required a SEND_SMS permission in our android manifest to send SMS. Following is the code snippet to set SEND_SMS permissions in manifest file.

 

<uses-permission android:name="android.permission.SEND_SMS"/>

Now we will see how to send SMS in android application using SMSManager API with examples.

Android Send SMS Example

Following is the example to send SMS using SMSManager api in android application.

 

Create a new android application using android studio and give names as SendSMSExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical" android:layout_width="match_parent"
   
android:layout_height="match_parent">
    <
TextView
       
android:id="@+id/fstTxt"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:layout_marginTop="150dp"
       
android:text="Mobile No" />
    <
EditText
       
android:id="@+id/mblTxt"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:ems="10"/>

    <TextView
       
android:id="@+id/secTxt"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Message"
       
android:layout_marginLeft="100dp" />
    <
EditText
       
android:id="@+id/msgTxt"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:ems="10" />
    <
Button
       
android:id="@+id/btnSend"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:text="Send SMS" />
</
LinearLayout>

Now open our main activity file MainActivity.java from \src\main\java\com.tutlane.sendsmsexample path and write the code like as shown below

MainActivity.java

package com.tutlane.sendsmsexample;
import android.content.Intent;
import android.net.Uri;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

   
private EditText txtMobile;
   
private EditText txtMessage;
   
private Button btnSms;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
txtMobile = (EditText)findViewById(R.id.mblTxt);
       
txtMessage = (EditText)findViewById(R.id.msgTxt);
       
btnSms = (Button)findViewById(R.id.btnSend);
       
btnSms.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
try{
                    SmsManager smgr = SmsManager.getDefault();
                    smgr.sendTextMessage(
txtMobile.getText().toString(),null,txtMessage.getText().toString(),null,null);
                    Toast.makeText(MainActivity.
this, "SMS Sent Successfully", Toast.LENGTH_SHORT).show();
                }
               
catch (Exception e){
                    Toast.makeText(MainActivity.
this, "SMS Failed to Send, Please try again", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

If you observe above code, we are sending SMS using SMSManager api on button click. As discussed, we need to add a SEND_SMS permission in our android manifest.

 

Now open android manifest file (AndroidManifest.xml) and write the code like as shown below

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.tutlane.sendsmsexample">
    <
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>

If you observe above AndroidManifest.xml file, we added a SEND_SMS permissions in manifest file.

Output of Android Send SMS Example

When we run above program in android studio we will get the result like as shown below.

 

Android Send SMS using SMSManager and Intents Example Result

 

Once enter all details and click on Send SMS button it will send SMS and show the alert message like as mentioned in above image. The above example we implemented using SMSManager API. In case if we want to use Intents to send SMS replace button click code like as shown below.

 

btnSms.setOnClickListener(new View.OnClickListener() {
   
@Override
    
public void onClick(View v) {
       
try{
            Intent i =
new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(
"smsto:"));
            i.setType(
"vnd.android-dir/mms-sms");
            i.putExtra(
"address", new String(txtMobile.getText().toString()));
            i.putExtra(
"sms_body",txtMessage.getText().toString());
            startActivity(Intent.createChooser(i,
"Send sms via:"));
        }
       
catch(Exception e){
            Toast.makeText(MainActivity.
this, "SMS Failed to Send, Please try again", Toast.LENGTH_SHORT).show();
        }
    }
});

This is how we can send SMS using either SMSManager API or Intent objects in android applications based on our requirements. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User