Send SMS in Android

Introduction

 
This article explains how to send a Short Message Service (SMS) message in Android. Android Studio is used to create the sample application.
 
SMS sends messages among various mobile devices over a network. You can send an SMS message in Android using an Intent. You need to write this code to send an SMS message in Android.
  1. String number = mobileno.getText().toString();  
  2. String message = message.getText().toString();  
  3. Intent i = new Intent(getApplicationContext(), MainActivity.class);  
  4. PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);  
  5. SmsManager sms = SmsManager.getDefault();  
  6. sms.sendTextMessage(no, null, msg, pIntent, null); 
What is an Intent?
 
An Intent is message ed among the components, such as Activities, BroadcastRecceiver, Services, ContentProviders and so on to perform an action in Android. Using it you can start an activity, receive a broadcast message, start a service and so on.
 
Intents are of two types
 
An Intent is one of the following two types:
  1. Implicit Intent
  2. Explicit intent
Implicit Intent
 
An Implicit Intent is an Intent that does not specify the component. The information will be available from the intent that is provided by the Android system. Using an intent you can open a web page.
 
Explicit Intent
 
An Explicit Intent is an Intent that specifies the component. Using this you can move from one activity to another.
 
What is PendingIntent
 
A PendingIntent is a token that to be provided to a foreign application, that allows the foreign application to use your application permissions to execute a predefined piece of code.
 
SMS Manager
 
The SMS Manager is a class that sends an SMS message from one device to another in Android. The SMS Manager class provides the getDefault() method to create the instance of it.
 
In this application, you use two editTexts in an XML file to enter a mobile number and a message that you want to send to the other device on a button click. In the Java class set the button on its clicklistener. Inside the onClick() method you will write the code to send an SMS message. In the Android Manifest.xml file, you will write the send SMS permission.
  1. <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>  
  2. <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> 
Step 1
 
Create a project as in the following:
 
Go to "File" -> "New project".
 
sendsms
 
Step 2
 
Create an XML file with the following:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#abcdef">  
  11.    
  12.     <TextView  
  13.         android:id="@+id/textView1"  
  14.         android:textStyle="bold"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="Mobile No:" />  
  18.    
  19.     <EditText  
  20.         android:id="@+id/editText1"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_alignParentRight="true"  
  24.         android:layout_alignParentTop="true"  
  25.         android:layout_marginRight="20dp"  
  26.         android:ems="10" />  
  27.    
  28.     <TextView  
  29.    
  30.         android:textStyle="bold"  
  31.         android:id="@+id/textView2"  
  32.         android:layout_width="wrap_content"  
  33.         android:layout_height="wrap_content"  
  34.         android:layout_marginTop="60dp"  
  35.         android:text="Message:" />  
  36.    
  37.     <EditText  
  38.         android:id="@+id/editText2"  
  39.         android:layout_width="wrap_content"  
  40.         android:layout_height="wrap_content"  
  41.         android:layout_alignLeft="@+id/editText1"  
  42.         android:layout_below="@+id/editText1"  
  43.         android:layout_marginTop="26dp"  
  44.         android:ems="10"  
  45.         android:inputType="textMultiLine" />  
  46.    
  47.     <Button  
  48.         android:layout_width="wrap_content"  
  49.         android:layout_height="wrap_content"  
  50.         android:text="Send Sms"  
  51.         android:id="@+id/btn_send"  
  52.         android:layout_centerInParent="true"></Button>  
  53. </RelativeLayout> 
Step 3
 
Create a Java class file with the following.
 
In the Java class, you will use code from both editText to set the button on its ClickListener. Now create the intent. this intent as an argument to create the PendingIntent. Call the getDefault() method to create the instance of the SMS Manager class. After this call the sendTextMessage() to the message to the other device. Inside the onClick() method you will write the code to send an SMS message. In the Android Manifest.xml file, you will write the send SMS permission.
  1. package com.sendsmsapplication;  
  2.    
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.app.PendingIntent;  
  6. import android.content.Intent;  
  7. import android.telephony.SmsManager;  
  8. import android.view.Menu;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.    
  15. public class MainActivity extends Activity {  
  16.    
  17.     EditText edittText1,edittText2;  
  18.     Button btn_Send;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         edittText1=(EditText)findViewById(R.id.editText1);  
  24.         edittText2=(EditText)findViewById(R.id.editText2);  
  25.         btn_Send=(Button)findViewById(R.id.btn_Send);  
  26.         //Performing action on button click  
  27.         btn_Send.setOnClickListener(new OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View arg0) {  
  30.                 String number=edittText1.getText().toString();  
  31.                 String message=edittText2.getText().toString();  
  32.                 Intent i=new Intent(getApplicationContext(),MainActivity.class);  
  33.                 PendingIntent pIntent=PendingIntent.getActivity(getApplicationContext(), 0, i,0);  
  34.                 SmsManager sms=SmsManager.getDefault();  
  35.                 sms.sendTextMessage(number, null, message, pIntent,null);  
  36.                 Toast.makeText(getApplicationContext(), "Message Sent !",  
  37.                         Toast.LENGTH_LONG).show();  
  38.             }  
  39.         });  
  40.     }  
  41.     @Override  
  42.     public boolean onCreateOptionsMenu(Menu menu) {  
  43.         // Inflate the menu; this adds items to the action bar if it is present.  
  44.         //getMenuInflater().inflate(R.menu.activity_main, menu);  
  45.         return true;  
  46.     }  
  47.    
Step 4
 
Android manifest. xml file
 
In the Android Manifest.xml file, you will write the send SMS permission.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.sendsmsapplication"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.sendsmsapplication.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.     <uses-permission  
  27.         android:name="android.permission.SEND_SMS"></uses-permission>  
  28.     <uses-permission android:name="android.permission.RECEIVE_SMS">  
  29.     </uses-permission>  
  30. </manifest> 
Step 5
 
Run this application on a real device.


Similar Articles