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

Loading Facebook Comments ...
Loading Disqus Comments ...