One thing Android does well is allow developers to interact with the phone. Between the TelephonyManager and the SmsManager, 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 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-sdkandroid:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS" />
<applicationandroid: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"
android:gravity="center"
android:layout_margin="10dip">
<TextViewandroid: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" />
<Buttonandroid: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; 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
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.