Você está na página 1de 10

15/07/2015

Android Messaging Example for Sending SMS Programmatically | Techblogon

Home
About us
Contact us
Privacy Policy

Tutorials
Gadgets
Internet
Technologies

Android Messaging Example for Sending SMS Programmatically


August 29, 2013 Android Tutorial, Tutorials

Simple way to Send SMS (Text Messages) from an Android Application.


Here we will discuss about how to send SMS from an Android application in a very simple way. Sending
SMS from an Android application programmatically is really very easy. Lets have a line by line discussion for
how to send massages in Android.

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

1/10

15/07/2015

Android Messaging Example for Sending SMS Programmatically | Techblogon

1. Create an Andeoid project named as : SendSMSExample


2. Add Package Name: com.techblogon.sendsmsexample
3. Select Target SDK version: API 17: Android 4.2 (Jelly Bean)
Note: I am using Eclipse Indigo, all the above data is for the example attached in this post. It might be different
options as per your Android setup. But dont worry it will work for all versions. For more clarity you can
download the example for Android Messaging Example for Sending SMS Programmatically from buttom
of this post.
To make this example very simple, we will take one Android Activity. Then we will take 2 EditText for getting
receipent number and composing SMS. Then we will use one more control as a buttom called Send to sending
the SMS to the destination number. I think this is fair enough to learn how to send the SMS in Android.
1. Add below SMS user permission in your Android Manifest file.
1

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

Your projects Manifest file will be looks like below.


AndroidManifest.xml
1
2
3

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.techblogon.sendsmsexample"

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

2/10

15/07/2015

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Android Messaging Example for Sending SMS Programmatically | Techblogon

android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.techblogon.sendsmsexample.SendSms"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

2. Add below xml file content in your source folder for GUI part. This xml file will hold 2 EditText and One
button for sending SMS. In my example the xml file name is activity_send_sms.xml.
Path: \res\layout\
File Name: activity_send_sms.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SendSms" >

<TextView
android:id="@+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Send SMS Programmatically" />

<EditText
android:id="@+id/editTextEnterReceipents"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_below="@+id/textViewTitle"
android:inputType ="number"
android:hint="Enter Receipent">
</EditText>

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

3/10

15/07/2015

28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

Android Messaging Example for Sending SMS Programmatically | Techblogon

<EditText
android:id="@+id/editTextCompose"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/editTextEnterReceipents"
android:layout_above="@+id/buttonSendNow"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:gravity="left|top"
android:hint="Type your message here" >
</EditText>

<Button
android:id="@+id/buttonSendNow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:onClick="onClickSend"
android:text="Send" />

</RelativeLayout>

3. Now coming to java source code for sending SMS from our Android application.
1
2
3
4
5
6
7
8

String strSMSBody = contentTextEdit.getText().toString();


String strReceipentsList = recipientTextEdit.getText().toString();
SmsManager sms = SmsManager.getDefault();
List&lt;String&gt; messages = sms.divideMessage(strSMSBody);
for (String message : messages) {
sms.sendTextMessage(strReceipentsList, null, message, PendingIntent.getBroadcast(

this, 0, new Intent(ACTION_SMS_SENT), 0), null);


}

Your projects Activity.Java file will looks like below. In my example it looks like below code snippet. You can
also get the delivery report and error code for the SMS you sent. Please refer to the below code.
Path: \src\com\techblogon\sendsmsexample\
File Name: SendSms.Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

package com.techblogon.sendsmsexample;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

4/10

15/07/2015

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

Android Messaging Example for Sending SMS Programmatically | Techblogon

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SendSms extends Activity {

public static final String ACTION_SMS_SENT = "com.techblogon.android.apis.os.SMS_SENT_ACTION"


EditText recipientTextEdit = null;
EditText contentTextEdit =null;
TextView titleTextView =null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_sms);

//Get GUI controls instance from here


recipientTextEdit = (EditText)this
.findViewById(R.id.editTextEnterReceipents);
contentTextEdit = (EditText)this
.findViewById(R.id.editTextCompose);
titleTextView = (TextView)this.findViewById(R.id.textViewTitle);

// Register broadcast receivers for SMS sent and delivered intents


registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = null;
boolean error = true;
switch (getResultCode()) {
case Activity.RESULT_OK:
message = "Message sent!";
error = false;
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
message = "Error.";
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
message = "Error: No service.";
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
message = "Error: Null PDU.";
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
message = "Error: Radio off.";
break;
}

recipientTextEdit.setText("");
contentTextEdit.setText("");
titleTextView.setText(message);
titleTextView.setTextColor(error ? Color.RED : Color.GREEN);

}
}, new IntentFilter(ACTION_SMS_SENT));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send_sms, menu);

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

5/10

15/07/2015

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

Android Messaging Example for Sending SMS Programmatically | Techblogon

return true;
}
public void onClickSend(View v)
{
//Get recipient from user and check for null
if (TextUtils.isEmpty(recipientTextEdit.getText())) {
titleTextView.setText("Enter Receipent");
titleTextView.setTextColor(Color.RED);
return;
}

//Get content and check for null


if (TextUtils.isEmpty(contentTextEdit.getText())) {
titleTextView.setText("Empty Content");
titleTextView.setTextColor(Color.RED);
return;
}
//sms body coming from user input
String strSMSBody = contentTextEdit.getText().toString();
//sms recipient added by user from the activity screen
String strReceipentsList = recipientTextEdit.getText().toString();
SmsManager sms = SmsManager.getDefault();
List<String> messages = sms.divideMessage(strSMSBody);
for (String message : messages) {
sms.sendTextMessage(strReceipentsList, null, message, PendingIntent.getBroadcast

this, 0, new Intent(ACTION_SMS_SENT), 0), null);


}
}

Note: In the above code snipt, you can also get SMS devilery report and error code if the SMS sending is failed.
4. Now build and run your application on Device and Emulator and enjoy Android coding.
Previous Page
Android Tutorial Home Page
Next Page

Post By SmrutiRanjan (57 Posts)

C ONNECT

Working @ Samsung as a Project Lead for Android


Smartphones. I have been blogging since 2008.
Previously I was writing articles for other bloggers, but
finally I have started my own blog-"Techblogon".I am also
an active contributor for the blog-"Gadgets n Gizmos
World". Job is my necessity, but blogging is my passion.
Website: Techblogon

3 Responses to Android Messaging Example for Sending SMS Programmatically


1.

Sudhansu says:

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

6/10

15/07/2015

Android Messaging Example for Sending SMS Programmatically | Techblogon

September 2, 2013 at 8:14 am


Hi,
I tried your sms sending code (APP code) but its not working. It gives error message.
I have copied all the code as it is, only package name is different (by default package name).
Please let me know what are the changes or addition i have to make so that it works.
Sudhansu
Reply
SmrutiRanjan says:
September 2, 2013 at 11:10 am
Hi Sudhanhu,
I have added new code. please check again.
Make sure to add the user permission in the andrpid manifest file?
android:name=com.techblogon.sendsmsexample.SendSms
Reply
2.

Srividhya says:
March 5, 2015 at 11:49 am
public static final String ACTION_SMS_SENT =
com.techblogon.android.apis.os.SMS_SENT_ACTION
USE OF THIS LINE
Reply

Leave a Reply
Your email address will not be published. Required fields are marked *
Name *
Email *
Website
nine 5 =

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

7/10

15/07/2015

Android Messaging Example for Sending SMS Programmatically | Techblogon

Comment
You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym
title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del
datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" dataurl=""> <span class="" title="" data-url="">

Post Comment

Search

Techblogon
Follow

+1

+ 305
Like 1,859 people like this. Be the first of your friends.

Recommend on Google

Follow @techblogon

216 follow ers

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

8/10

15/07/2015

Android Messaging Example for Sending SMS Programmatically | Techblogon

Enter your Email Address...


Subscribe

RSS Feed

Google Plus

Archives
March 2015
July 2014
April 2014
October 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012

Categories
Android
Android Tutorial
Design Patterns
Displays
E-Commerce
Gadgets
Google
Internet
Mobile Phones
Softwares
Technologies
Tutorials
Windows

Pages
About us
Contact us
Privacy Policy
http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

9/10

15/07/2015

Android Messaging Example for Sending SMS Programmatically | Techblogon

Meta
Log in
Entries RSS
Comments RSS
WordPress.org

2015 Techblogon

http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/

10/10

Você também pode gostar