Vous êtes sur la page 1sur 7

How to send a text message from within your Android app - TechRepublic http://www.techrepublic.com/blog/software-engineer/how-to-send-a-tex...

APPS OPTIMIZE

How to send a text message from within your Android app


By William J. Francis in Software Engineer, June 25, 2013, 3:22 AM PST

Get the word out about your Android app by sending a text message within the
application. Here's how to do just with the help of the Android SmsManager
class.

One thing Android (http://developer.android.com/develop/index.html) does well is allow developers to interact with the phone. Between
the TelephonyManager (http://developer.android.com/reference/android/telephony/TelephonyManager.html) and the SmsManager
(http://developer.android.com/reference/android/telephony/SmsManager.html), the Android SDK usually has you covered. In the tutorial that
follows, we will send a text message directly from within our application. Best of all, thanks to the SmsManager, the code that
does the actual transmission of the SMS message fits on a single line.

Feel free to follow along with the step-by-step instructions below, or, download the entire project (http://b2b.cbsimg.net/downloads
/Weilage/hello_sms.zip) and import it directly into Eclipse.

1. Create a new Android project in Eclipse. Target Android 2.2 or higher.

2. In order to send a text message from an application, the manifest must declare the SEND_SMS permission. Open the
AndroidManifest.xml and add the permission just after the SDK declarations.

AndroidManifest.xml

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


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

package="com.authorwjf.hellosms"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"

android:targetSdkVersion="17" />

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

<application

1 of 7 11/15/2013 4:22 PM
How to send a text message from within your Android app - TechRepublic http://www.techrepublic.com/blog/software-engineer/how-to-send-a-tex...

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.authorwjf.hellosms.MainActivity"

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>

3. In the /res/layout folder, create a new linear layout to represent our user interface. We need a text view, edit text, and a
button.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

2 of 7 11/15/2013 4:22 PM
How to send a text message from within your Android app - TechRepublic http://www.techrepublic.com/blog/software-engineer/how-to-send-a-tex...

android:gravity="center"
android:layout_margin="10dip">

<TextView
android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Enter A Phone Number:"


android:textSize="20sp" />

<EditText
android:id="@+id/editView1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"
android:layout_margin="20dip" />

<Button
android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Say Hello!" />

</LinearLayout>

4. Now it is time to modify the MainActivity.java source file. Use the onCreate to wire up the button and implement an onClick
override to engage the SmsManager. Check out the call to the SmsManager inside of our try / catch block. As promised, a
single line of code is all it takes to send the text message to the destination of your choosing.

MainActivity.java

package com.authorwjf.hellosms;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;

3 of 7 11/15/2013 4:22 PM
How to send a text message from within your Android app - TechRepublic http://www.techrepublic.com/blog/software-engineer/how-to-send-a-tex...

import android.widget.EditText;
import android.app.Activity;
import android.app.AlertDialog;

public class MainActivity extends Activity implements OnClickListener{

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

findViewById(R.id.button1).setOnClickListener(this);
}

@Override
public void onClick(View v) {
String phoneNumber = ((EditText)

findViewById(R.id.editView1)).getText().toString();
try {
SmsManager.getDefault().sendTextMessage(phoneNumber, null, "Hello

SMS!", null, null);


} catch (Exception e) {
AlertDialog.Builder alertDialogBuilder = new

AlertDialog.Builder(this);
AlertDialog dialog = alertDialogBuilder.create();

dialog.setMessage(e.getMessage());

dialog.show();

The code is ready to compile and run (Figure A). While it runs on the emulator, you need to load it to an actual device to
send an SMS message. Type your own phone number into the text field and say hello to yourself. Figure A

4 of 7 11/15/2013 4:22 PM
How to send a text message from within your Android app - TechRepublic http://www.techrepublic.com/blog/software-engineer/how-to-send-a-tex...

(http://b2b.cbsimg.net/blogs/8675309.png)

While at first glance, it may seem curious that you'd want your app to send a text message at all when a perfectly good text
application ships with the operating system, there are quite a few valid scenarios for using the SmsManager. I use this
capability as an easy way for users to recommend my app to their friends. As long as you are careful not to abuse the
privilege, text messages are a powerful way to spread the word about your app.

Sign up for TechRepublic's App Builder newsletter!

About William J. Francis


William J Francis began programming computers at age eleven. Specializing in embedded and mobile
platforms, he has more than 20 years of professional software engineering under his belt, including a four year
stint in the US Army's Military Intellige...

5 of 7 11/15/2013 4:22 PM
How to send a text message from within your Android app - TechRepublic http://www.techrepublic.com/blog/software-engineer/how-to-send-a-tex...

Add your Comment

Editor's Picks

Windows 8 tablets 10 reasons to avoid


really do have a future using PST files

Career advice for What NSA spying on


Millennials: Hack into Google means for your
the workplace business

White Papers, Webcasts, and Downloads

6 of 7 11/15/2013 4:22 PM
How to send a text message from within your Android app - TechRepublic http://www.techrepublic.com/blog/software-engineer/how-to-send-a-tex...

Don't Miss Our Latest Updates


Editor's Daily Picks

Week in Review

It's Microsoft Patch Tuesday: November 2013


Microsoft // November 13, 2013, 1:48 PM PST

Just the two of us: Why global companies should aim for twin datacentres
Data Centers // November 5, 2013, 5:07 AM PST

ICANN delegates new generic top-level domains


Web Development // November 1, 2013, 5:44 AM PST

7 of 7 11/15/2013 4:22 PM

Vous aimerez peut-être aussi