Académique Documents
Professionnel Documents
Culture Documents
APPS OPTIMIZE
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.
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
package="com.authorwjf.hellosms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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>
</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"
<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"
</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;
@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
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.
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...
Editor's Picks
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...
Week in Review
Just the two of us: Why global companies should aim for twin datacentres
Data Centers // November 5, 2013, 5:07 AM PST
7 of 7 11/15/2013 4:22 PM