Skip to main content

How to send sms in android

We can send sms in android via intent. You need to write only 4 lines of code the send sms in android.
  1. //Getting intent and PendingIntent instance  
  2. Intent intent=new Intent(getApplicationContext(),MainActivity.class);  
  3. PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  
  4.   
  5. //Get the SmsManager instance and call the sendTextMessage method to send message                 
  6. SmsManager sms=SmsManager.getDefault();  
  7. sms.sendTextMessage("8802177690"null"hello javatpoint", pi,null);  

Example of sending sms in android

activity_main.xml

Drag the 2 edittexts, 2 textviews and 1 button from the pallete, now the activity_main.xml file will like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="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.     tools:context=".MainActivity" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginRight="20dp"  
  14.         android:ems="10" />  
  15.   
  16.     <EditText  
  17.         android:id="@+id/editText2"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_alignLeft="@+id/editText1"  
  21.         android:layout_below="@+id/editText1"  
  22.         android:layout_marginTop="26dp"  
  23.         android:ems="10"  
  24.         android:inputType="textMultiLine" />  
  25.   
  26.     <TextView  
  27.         android:id="@+id/textView1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_alignBaseline="@+id/editText1"  
  31.         android:layout_alignBottom="@+id/editText1"  
  32.         android:layout_toLeftOf="@+id/editText1"  
  33.         android:text="Mobile No:" />  
  34.   
  35.     <TextView  
  36.         android:id="@+id/textView2"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_alignBaseline="@+id/editText2"  
  40.         android:layout_alignBottom="@+id/editText2"  
  41.         android:layout_alignLeft="@+id/textView1"  
  42.         android:text="Message:" />  
  43.   
  44.     <Button  
  45.         android:id="@+id/button1"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_height="wrap_content"  
  48.         android:layout_alignLeft="@+id/editText2"  
  49.         android:layout_below="@+id/editText2"  
  50.         android:layout_marginLeft="34dp"  
  51.         android:layout_marginTop="48dp"  
  52.         android:text="Send SMS" />  
  53.   
  54. </RelativeLayout>  

Write the permission code in Android-Manifest.xml file

You need to write SEND_SMS permission as given below:
  1. <uses-permission android:name="android.permission.SEND_SMS"/>  
  2.    
File: Android-Manifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest   
  3. xmlns:androclass="http://schemas.android.com/apk/res/android"  
  4.     package="com.example.sendsms"  
  5.     android:versionCode="1"  
  6.     android:versionName="1.0" >  
  7.   
  8.     <uses-sdk  
  9.         android:minSdkVersion="8"  
  10.         android:targetSdkVersion="16" />  
  11.   
  12.     <uses-permission android:name="android.permission.SEND_SMS"/>  
  13.     
  14.     <uses-permission android:name="android.permission.RECEIVE_SMS"/>  
  15.   
  16.     <application  
  17.         android:allowBackup="true"  
  18.         android:icon="@drawable/ic_launcher"  
  19.         android:label="@string/app_name"  
  20.         android:theme="@style/AppTheme" >  
  21.         <activity  
  22.             android:name="com.example.sendsms.MainActivity"  
  23.             android:label="@string/app_name" >  
  24.             <intent-filter>  
  25.                 <action android:name="android.intent.action.MAIN" />  
  26.   
  27.                 <category android:name="android.intent.category.LAUNCHER" />  
  28.             </intent-filter>  
  29.         </activity>  
  30.     </application>  
  31.   
  32. </manifest>  

Activity class

Let's write the code to make the phone call via intent.
File: MainActivity.java
  1. package com.example.sendsms;  
  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 mobileno,message;  
  18.     Button sendsms;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.           
  24.         mobileno=(EditText)findViewById(R.id.editText1);  
  25.         message=(EditText)findViewById(R.id.editText2);  
  26.         sendsms=(Button)findViewById(R.id.button1);  
  27.           
  28.     //Performing action on button click  
  29.         sendsms.setOnClickListener(new OnClickListener() {  
  30.               
  31.             @Override  
  32.             public void onClick(View arg0) {  
  33.                 String no=mobileno.getText().toString();  
  34.                 String msg=message.getText().toString();  
  35.                   
  36.                 //Getting intent and PendingIntent instance  
  37.                 Intent intent=new Intent(getApplicationContext(),MainActivity.class);  
  38.                 PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0, intent,0);  
  39.                   
  40.                 //Get the SmsManager instance and call the sendTextMessage method to send message  
  41.                 SmsManager sms=SmsManager.getDefault();  
  42.                 sms.sendTextMessage(no, null, msg, pi,null);  
  43.                   
  44.                 Toast.makeText(getApplicationContext(), "Message Sent successfully!",  
  45.                     Toast.LENGTH_LONG).show();  
  46.             }  
  47.         });  
  48.     }  
  49.   
  50.     @Override  
  51.     public boolean onCreateOptionsMenu(Menu menu) {  
  52.         // Inflate the menu; this adds items to the action bar if it is present.  
  53.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  54.         return true;  
  55.     }  
  56.       
  57. }  


Output:

android send sms example output 1

android send sms example output 2

Comments

Popular posts from this blog

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM Abstract Allocating service capacities in cloud computing is based on the assumption that they are unlimited and can be used at any time. However, available service capacities change with workload and cannot satisfy users’ requests at any time from the cloud provider’s perspective because cloud services can be shared by multiple tasks. Cloud service providers provide available time slots for new user’s requests based on available capacities. In this paper, we consider workflow scheduling with deadline and time slot availability in cloud computing. An iterated heuristic framework is presented for the problem under study which mainly consists of initial solution construction, improvement, and perturbation. Three initial solution construction strategies, two greedy- and fair-based improvement strategies and a perturbation strategy are proposed. Different strategies in the three phases result in several heuristics. ...

MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework

            MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework ABSTRACT  In recent years, recommendation systems have seen significant evolution in the field of knowledge engineering. Most of the existing recommendation systems based their models on collaborative filtering approaches that make them simple to implement. However, performance of most of the existing collaborative filtering-based recommendation system suffers due to the challenges, such as: (a) cold start, (b) data sparseness, and (c) scalability. Moreover, recommendation problem is often characterized by the presence of many conflicting objectives or decision variables, such as users’ preferences and venue closeness. In this paper, we proposed MobiContext , a hybrid cloud-based Bi-Objective Recommendation Framework (BORF) for mobile social networks. The MobiContext utilizes multi-objective optimization techniques to generate personalized recommendat...