I found several nice links [here and here] showing how to do SMS handling in Android. However those examples all seem to be specific to a pre-1.0 version of the SDK. SDK versions 1.1 and 1.5 have a slightly different way of handling things. This blog post will show you how to get an asynchronous notification when new SMS messages arrive on your phone, for the Android 1.1 and 1.5 SDKs. Thanks to Cristina for posting this method to the android-developers group.
If you remember your Android Fundamentals, you will recall that " ... broadcast receivers are activated by asynchronous messages called intents".
The first thing we must therefore do is to register a new broadcast receiver in our "AndroidManifest.xml" file. Additionally we must request the "android.permission.RECEIVE_SMS" permission from the framework. See the following complete listing for "AndroidManifest.xml".
1 <?xml version="1.0" encoding="utf-8"?>Now we must create a class that extends BroadcastReceiver. BroadcastReceiver is abstract, and we must override the "onReceive" method.
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="sg.aarvark.android"
4 android:versionCode="1"
5 android:versionName="1.0">
6 <application android:icon="@drawable/icon"
7 android:label="@string/app_name" android:debuggable="true">
8 <activity android:name=".SMSDroid"
9 android:label="@string/app_name">
10 <intent-filter>
11 <action android:name="android.intent.action.MAIN" />
12 <category android:name="android.intent.category.LAUNCHER" />
13 </intent-filter>
14 </activity>
15 <receiver android:name=".SMSReceiver">
16 <intent-filter>
17 <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
18 </intent-filter>
19 </receiver>
20 </application>
21 <uses-sdk android:minSdkVersion="3" />
22 <uses-permission android:name="android.permission.RECEIVE_SMS"/>
23 </manifest>
SMS Messages are delivered to the phone in the PDU (Protocol Description Unit) format. Our goal is therefore to
- Get the PDUs representing our incoming SMS message
- Decode the PDUs to get readable SMS messages
1 public class SMSReceiver extends BroadcastReceiver {
2
3 private static final String LOG_TAG = "SMS_FUNKY";
4 private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
5
6 @Override
7 public void onReceive(Context context, Intent intent) {
8
9 if (intent.getAction().equals(ACTION)){
10 Bundle bundle = intent.getExtras();
11
12 Object[] pdusObj = (Object[]) bundle.get("pdus");
13
14
15 SmsMessage[] messages = new SmsMessage[pdusObj.length];
16 for (int i=0; i <pdusObj.length; i++){
17 messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
18
19 }
20
21 StringBuilder sb = new StringBuilder();
22 for (SmsMessage currentMessage: messages){
23 sb.append("Received SMS Message\nFrom: ");
24 sb.append(currentMessage.getDisplayOriginatingAddress());
25 sb.append("\n----Message----\n");
26 sb.append(currentMessage.getDisplayMessageBody());
27 }
28
29 Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
30
31 }
32
33 }
34
35 }
The above code is fairly self-explanatory. You can download the
complete Eclipse project, which is based on the HelloAndroid example in the
Android Dev Guide from HERE.
0 comments:
Post a Comment