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

Jio

Reliance Jio planning its own  cryptocurrency called JioCoin  elder son Akash Ambani leading the JioCoin project, Reliance Jio plans to build a 50-member team of young professionals to work on blockchain technology, which can also be used to develop applications such as smart contracts and supply chain management logistics

PUNCHING MACHINE

ACCIDENT AVOIDING SYSTEM FOR PUNCHING MACHINE SYNOPSIS The aim of our project is to take a system-wide approach to preventing the machine accident. The system includes not just the machine and the operator; but rather, it includes everything from the initial design of the machine to the training of everyone that is responsible for any aspect of it, to the documentation of all changes, to regular safety audits and a finally a corporate culture of safety-first. Design is the part of a machine's life where the greatest impact can be made in relation to avoiding accidents. The designer should ensure that the machine is safe to set up and operate, safe to install, safe to maintain, safe to repair, and safe to decommission. Although safe operation is usually at the forefront of a designer's mind, safe maintenance and repair should also be a high priority. Around 50% of fatal accidents involving industrial equipment are associated with maintenance activities, and design...