Escolar Documentos
Profissional Documentos
Cultura Documentos
Home
About us
Contact us
Privacy Policy
Tutorials
Gadgets
Internet
Technologies
http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/
1/10
15/07/2015
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: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" />
</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
<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
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
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_sms);
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
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;
}
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
C ONNECT
Sudhansu says:
http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/
6/10
15/07/2015
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
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
http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/
8/10
15/07/2015
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
Meta
Log in
Entries RSS
Comments RSS
WordPress.org
2015 Techblogon
http://techblogon.com/android-messaging-example-for-sending-sms-programmatically/
10/10