Android – Sending SMS from android application – Method 2

By -

Hi,

Here i am going to discuss about to send SMS from your android application.

Basically there are 2 ways by which we can send sms:

  1. Open native SMS composer
  2. write your message and send from your android application

So here i am going to write about the 2nd method i.e. write your message and send from your android application itself.

(For your information, Method 1 – Send SMS by opening native SMS Composer)

Main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
	android:id="@+id/scrollview1"
	android:layout_height="fill_parent"
	android:layout_width="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
	<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <TextView
        	android:layout_width="wrap_content"
        	android:text="Enter mobile number:"
        	android:id="@+id/textView1"
        	android:layout_alignParentLeft="true"
        	android:layout_height="wrap_content">
        </TextView>
        <EditText
        	android:id="@+id/etextMblNumber"
        	android:layout_width="fill_parent"
        	android:layout_height="wrap_content"
        	android:layout_below="@+id/textView1"
        	android:inputType="number">
        </EditText>
         <TextView
        	android:layout_width="wrap_content"
        	android:text="Enter message:"
        	android:id="@+id/textView2"
        	android:layout_height="wrap_content"
        	android:layout_below="@+id/etextMblNumber">
        </TextView>
        <EditText
        	android:id="@+id/etextMsg"
        	android:layout_width="fill_parent"
        	android:layout_height="250dip"
			android:layout_below="@+id/textView2"
			android:gravity="top">
        </EditText>
        <Button
        	android:id="@+id/btnSendSMS"
        	android:layout_height="wrap_content"
        	android:layout_width="wrap_content"
        	android:text="Send SMS"
        	android:layout_below="@+id/etextMsg"
        	android:layout_centerInParent="true">
        </Button>
	</RelativeLayout>
</ScrollView>

SendSMSActivity

package com.technotalkative.sendsms;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class SendSMSActivity extends Activity {
    /** Called when the activity is first created. */
	EditText eTextMsg, eTextMblNumber;
	Button btnSendSMS;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        eTextMblNumber = (EditText) findViewById(R.id.etextMblNumber);
        eTextMsg = (EditText) findViewById(R.id.etextMsg);
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        btnSendSMS.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sendSMS();
			}
        });
    }
    public void sendSMS()
    {
    	SmsManager sm = SmsManager.getDefault();
		String number = eTextMblNumber.getText().toString();
		String msg = eTextMsg.getText().toString();
		sm.sendTextMessage(number, null, msg, null, null);
    }
}

Now, The main thing which we should not forget to add is the SEND_SMS permission inside the AndroidManifest.xml file:

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

 

Paresh Mayani

Paresh Mayani is a lead android developer from India. Currently, He is Sr. Software engineer at InfoStretch Solutions Pvt. Ltd. He has been developing android apps since very 1st version i.e. Android 1.5, he says he has seen days and nights of Android history. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

  • http://blogger.robin.com Robbes

    Hello Paresh.
    Well i am developing similar kind of application.
    My application has four tabs, namely home,Compose,Inbox and Sent.
    Home Tab: shall contain information abt my apps.
    Compose Tab: shall contain similar compose SMS
    Inbox Tab: shall contain SMS recieved from server based on sms keyword which will be a listview of sms recieved and shall have gmail inbox look alike feature.
    Sent Tab: shall contain same as inbox tab except this will have listview of sms sent.

    I want to know as to how to have a gmail inbox look like featuring implementing in the Inbox tab.
    Any help will be appreciated alot

    • http://pareshnmayani.wordpress.com Paresh N Mayani

      As you have mentioned regarding the application that you want to/supposed to develop, but you know my dear this is the large topic of discussion. Anyway, let me mention about to create SMS Listview type of screen.

      As you have mentioned that you will fetch SMS/email from the server based on the sms keyword, for that you need to make webservice call and parsing functionality. For that you need to know about calling web service and parsing technique. And then i think its easy to implement the Custom ListView to display available sms. For defining custom listview,i had already written around 6-7 articles previously so i suggest you to go through those.

  • Pingback: Android – Sending SMS from android application – Method 1 » TechnoTalkative

  • http://www.facebook.com/arief.spekta Arief Rivai

    Please post receive SMS programatically.. Thanks!

  • Shivani Bhoyar

    I am new to android..I tried This code bt not getting result.. Can u plz show manifest file..

    • http://www.technotalkative.com/ Paresh Mayani

      As i have mentioned in article, no need to do anything other than including SEND_SMS permission.

      • Shivani Bhoyar

        Still its not working.. Not showing ny error also.. Beside this code nything else needs to b done..

  • Madhav

    Shivani r u running this code on emulator.
    ny help plz mail madhavglbitm@gmail.com