RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Let Your Android Application Out of the Box with SMS Integration  : Page 3

Google’s Android allows developers a great deal of access to the phone. Enable your applications to communicate with the outside world using SMS messages.

Sending an SMS
So far, you've learned how to listen for and respond to incoming SMS messages. To close the loop, you'll have to be able to send text messages. As an example, you'll extend the SMS listener so that it will report the GPS coordanates of the phone.

Where Did I Leave My Phone?
As a means to demonstrate sending an SMS message, you'll have to extend your SMS listener application to report, through text messaging, where it is. Because you're already listening for incoming text messages, you can just flesh out the function alluded to above. You'll just need to finish getGPSData() and sendGPSData(). Because this article isn't about position locations, you're going to be on your own for the GPS stuff. You can, however, check out one of my previous DevX articles for information on completing a GPS position lookup.

Assume, for the sake of simplicity and expediency, that you have used the phone's GPS module to obtain it's location. Further assume that you'll need to send that data back to the device requesting it. Android makes it very easy to build and send SMS messages. Listing 2 shows the code to do so.

Simple, right? Okay, maybe not so much. You'll need to build a string to send off in the text message, using the predetermined lat and lon address. You'll also need to grab the default SmsManager instance. You must, with the 0.9 version of the SDK, create a PendingIntent that can eventually be passed into the sendTextMessage call; more on that in a second. Next, you'll want to grab the return address on the incoming text message so you can address the SMS back out.

With all these pieces in place its time to call sendTextMessage.

Author's Note: Again, you must use the current version of the SDK to pass in a valid pointer to a PendingIntent. The documentation says you don't have to, but without it, you'll get a null pointer exception and your send will fail. With that, your messages should start flowing. The docs say you can send messages between instances of the Emulator by using the port (default: 5556) as the address. More on that here.

Unwrapping Up
In this article, you learned how to register as a listener for incoming SMS messages in Android. Once you're receiving incoming text messages you can filter them for a few trigger phrases. You also explored, by example, two such triggers. One trigger, upon receiving the correct trigger, launched a specific Activity. The other, when queried as to the location of the phone, used the GPS system to return a text message with the latitude and longitude of the phone. The examples shown here are by no means complete or polished applications. However, you should be able to use them to build your own framework for receiving, interpreting, and sending text messages in your own application. Now, let that Android application of yours out of the box. Go code.

Chris Haseman is an independent software engineer specializing in wireless development. He can be found riding his bike between coffee shops in San Francisco. He's the author of the book Android Essentials (published by Apress). In his spare time, he's a resident DJ at xtcradio.com and a martial arts instructor.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date