ushing data to and from a third-party mobile application on demand has always been a tricky proposition. BREW allows, with the blessing of Qualcoom, SMS messages to be directed at a particular application resident on the phone. Apple's iPhone messaging platform, which, as of press time, is still in testing, allows for similar functionality through HTTP push messaging. As for Java ME, well, we're just not going to talk about Java ME. Doing data push and remote application activation with Android is somewhat similar to BREW's directed SMS process. By the time you finish this article, you should have a steady grasp of how to send and receive SMS messages from your application in Android.
You'll explore the SMS functionality in two ways. First, you'll set up a BroadcastReceiver to intercept and inspect SMS messages as they are delivered to the phone. Once you've received them you'll be able to inspect their contents, activate an application, process data, or, as we'll explore in the second part, send a return SMS message.
| Author's Note: This code for this article is based on version 1.0 BETA of the Android SDK, and I'd recommend an update to the latest version of Android before trying any of the code below.|
Receiving SMS Messages
Receiving and reacting to SMS events can be broken down into two tasks: registering for SMS_RECIEVED intents through the AndroidManifest.xml file, and writing code that combs through those events looking for specific text.
Registering for SMS BroadcastIntents
For you copy/paste programmers out there, you can find the entire contents of this test application here.
With absolutely nothing else, here's what the AndroidManifest.xml file looks like:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name=".SmsIntentReceiver" android:enabled="true">
The file contains a few notable pieces of information. First, but not necessarily foremost, is the <uses-permissions>
tag. This line must appear within the manifest tag but before the definition of the application. This tells Android that you'll be using the RECEIVE_SMS
permissions. Currently, Android has not announced how it will deal with user notifications and permission filtering. For now, it's enough to know that the application won't work without these permission declarations.
Second, is the <receiver> tag. This block of XML tells the Android OS the name of the class to load, in this case SmsIntentReceiver, and which BroadcastIntents on which to load it. You'll tell Android which intents to send us by registering for <action> tags inside the <intent-filter> tag. In this case, you'll register for the android.provider.Telephony.SMS_RECIEVED intent. This intent is fired each time an SMS hits the phone.
| Author's Note: Currently, Android does not allow one BroadcastReceiver to accept and consume an Intent. This is a major technical hurdle that must be overcome before Android is released into the world. Because you cannot consume SMS message intents, both the application and Android's native SMS application will receive the incoming SMS message. As you'll see later, this means you'll have to keep filter text for the SMS messages human readableas they'll end up in the inbox of the person receiving the message.|