Send SMS from android application example


                Program to send SMS in Android is very simple to develop, its hardly two lines. Before explaining the code, left me give some basics about the class used in this example.

SmsManager

                Class that manages Android SMS operations such as sending data, text. We need to create object by calling the static method SmsManager.getDefault()

SmsManager.getDefault() - will get default instance of SmsManager 



Using this instance we need to call sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
For now, lets concentrate on two parameters in this method.

scAddress - This is used to specify the SMS service center to use. If you enter null, the default service center for the device carrier will be used.
destinationAddress - is the target phone number you wish to text
text - message to be send

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);

That's it, simple it is..

Permission
                  Another critical part is to update Androidmanifest.xml, by adding this code to it

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

This grants permission for Android application to send sms.

Possible Exception: 

                 If you miss to add permission to SEND_SMS in Androidmanifest.xml, then you will receive error while sending message
"Sending sms message: Neither user nor current process has android.permission.SEND_SMS"

Output Screen
SMS Application


Code - Download complete project here


protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  btnSend = (Button) findViewById(R.id.send);
  etPhoneNo = (EditText) findViewById(R.id.phno);
  etMsg = (EditText) findViewById(R.id.smstxt);
  
  btnSend.setOnClickListener(new OnClickListener() {    
   @Override
   public void onClick(View v) { 
     String phoneNo = etPhoneNo.getText().toString();
     String msg = etMsg.getText().toString(); 
     try {      
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, msg, null, null);    
    Toast.makeText(getApplicationContext(), "Message Sent",
       Toast.LENGTH_LONG).show();
     } catch (Exception ex) {
    Toast.makeText(getApplicationContext(),
     ex.getMessage().toString(),
     Toast.LENGTH_LONG).show();
    ex.printStackTrace();
     } 
   }
  });  
 }

Thats it, its very simple enough to send sms in android application programmatically.

To confirm SMS/Message sent action and delivery in android, have a look at this example

 



Reactions:

4 comments :

  1. excuse me...
    please help me...
    Do you have recommendations about MMS on Android reference?

    Tahnkyu

    ReplyDelete
  2. Can please help me with recieve SMS too..<3..thnks:)

    ReplyDelete
  3. need to send sms and save them as a sent items for conversations any help?

    ReplyDelete
  4. How to send sms by choosing contact from contact list..

    ReplyDelete