Sending SMS Messages

To send a text message from your application, you will add the <uses-permission android:name="android.permission.SEND_SMS" /> permission to your manifest file and then use the android.telephony.gsm.SmsManager class (see Listing 9-6).

Listing 9-6. Sending SMS (Text) Messages import android.app.Activity;

import android.os.Bundle;

import android.telephony.gsm.SmsManager;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class TelephonyDemo extends Activity

private static final String TAG = "TelephonyDemo"; ^Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.sms);

Button sendBtn = (Button)findViewById(R.id.sendSmsBtn); sendBtn.setOnClickListener(new OnClickListener(){ ^Override public void onClick(View view) { EditText addrTxt = (EditText)TelephonyDemo.this.findViewById(R.id.addrEditText);

EditText msgTxt = (EditText)TelephonyDemo.this.findViewById(R.id.msgEditText);

sendSmsMessage( addrTxt.getText().toString(),msgTxt.getText().toString());

Toast.makeText(TelephonyDemo.this, "SMS Sent", Toast.LENGTH_LONG).show();

Toast.makeText(TelephonyDemo.this, "Failed to send SMS",

^Override protected void onDestroy() { super.onDestroy();

private void sendSmsMessage(String address,String message)throws Exception {

SmsManager smsMgr = SmsManager.getDefault(); smsMgr.sendTextMessage(address, null, message, null, null);

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Destination Address:" />

<EditText android:id="@+id/addrEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9045551212" />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text Message:" />

<EditText android:id="@+id/msgEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello sms" />

</LinearLayout>

<Button android:id="@+id/sendSmsBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Send Text Message" />

</LinearLayout>

The example in Listing 9-6 demonstrates sending SMS text messages using the Android SDK. Looking at the layout snippet first, you can see that the user interface has two EditText fields: one to capture the SMS recipient's destination address (the phone number), and another to hold the text message. The user interface also has a button to send the SMS message, as shown in Figure 9-7.

Media Demo

Destination Address: fcl'E

Text Message: |H

Send Text Message

Figure 9-7. The UI for the SMS example

The interesting part of the sample is the sendSmsMessage() method. The method uses the SmsManager class's sendTextMessage() method to send the SMS message. Here's the signature of SmsManager.sendTextMessage():

sendTextMessage(String destinationAddress, String smscAddress, String textMsg, PendingIntent sentIntent, PendingIntent deliveryIntent);

In this example, you populate only the destination address and the text-message parameters. You can, however, customize the method so it doesn't use the default SMS center (the address of the server on the cellular network that will dispatch the SMS message). You can also implement a customization in which pending intents are called when the message is sent and a delivery notification has been received.

All in all, sending an SMS message is about as simple as it gets with Android. Realize that, with the emulator, your SMS messages are not actually sent to their destinations. You can, however, assume success if the sendTextMessage() method returns without an exception. As shown in Listing 9-6, you use the Toast class to display a message in the UI to indicate whether the SMS message was sent successfully.

Sending SMS messages is only half the story. Now we'll show you how to monitor incoming SMS messages.

0 0

Post a comment