This Tutorial was inspired by the nice, but almost uncommented davanum Apache Blog.
Recognize/React on incoming SMS
What you will learn: You will learn how to recognize/react on incoming SMS by using a IntentReceiver. A possible scenario is, to play a music when there is an incoming SMS or modify the SMS.
Difficulty: 2 of 5

What it will look like:
Think away the 'compressed' as this screenshot is from my sms-compression app
Screenshot was taken with sdk-version m3 (m5 looks similar)

Screenshot was taken with sdk-version m3 (m5 looks similar)

Description:
0.)What we want to do is to react on the Intent "android.provider.Telephony.SMS_RECEIVED", which is fired by the System when there is an Incoming SMS. This will be done by an 'passive' IntentReceiver. This IntentReceiver will show a Notification (like in the picture above) and start a Main-Activity afterwards.
Note that we require a permission in the AndroidManufest.xml to receive the incoming sms:
Using xml Syntax Highlighting
- <uses-permission android:name="android.permission.RECEIVE_SMS" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
This is the full AndroidManufest.xml:
Using xml Syntax Highlighting
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="org.anddev.android.smsexample">
- <uses-permission android:name="android.permission.RECEIVE_SMS" />
- <application android:icon="@drawable/icon">
- <!-- The Main Activity that gets started by the IntentReceiver listed below -->
- <activity android:name=".SMSActivity" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <!-- This class will react on the SMS show a notification
- and start the Main-App afterwards -->
- <receiver android:name=".SMSReceiver">
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
- </application>
- </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
1.)So we now need to implement the IntentReceiver we call "SMSReceiver" and the MainActivity we call "SMSActivity".
Lets workout the "SMSReceiver" first:
The uninteresting parts first:
Using java Syntax Highlighting
- package org.anddev.android.smsexample;
- import android.app.NotificationManager;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentReceiver;
- import android.os.Bundle;
- import android.provider.Telephony;
- import android.telephony.gsm.SmsMessage;
- import android.util.Log;
- public class SMSReceiver extends IntentReceiver {
- /** TAG used for Debug-Logging */
- private static final String LOG_TAG = "SMSReceiver";
- /** The Action fired by the Android-System when a SMS was received.
- * We are using the Default Package-Visibility */
- private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
:idea: This is the method, that is called when the IntentReceiver is 'invoked' by the System. This just happens, becuase we exposed this Class in the AndroidManifest.xml 
Using java Syntax Highlighting
- // @Override
- public void onReceiveIntent(Context context, Intent intent) {
- if (intent.getAction().equals(ACTION)) {
- // if(message starts with SMStretcher recognize BYTE)
- StringBuilder sb = new StringBuilder();
- /* The SMS-Messages are 'hiding' within the extras of the Intent. */
- Bundle bundle = intent.getExtras();
- if (bundle != null) {
- /* Get all messages contained in the Intent*/
- SmsMessage[] messages =
- Telephony.Sms.Intents.getMessagesFromIntent(intent);
- /* Feed the StringBuilder with all Messages found. */
- for (SmsMessage currentMessage : messages){
- sb.append("Received compressed SMSnFrom: ");
- /* Sender-Number */
- sb.append(currentMessage.getDisplayOriginatingAddress());
- sb.append("n----Message----n");
- /* Actual Message-Content */
- sb.append(currentMessage.getDisplayMessageBody());
- }
- }
- /* Logger Debug-Output */
- Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
- /* Show the Notification containing the Message. */
- Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Finally we start the Main-Activity doing the following:
Using java Syntax Highlighting
- /* Start the Main-Activity */
- Intent i = new Intent(context, SMSActivity.class);
- i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
- context.startActivity(i);
- }
- }
- }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
2.) The SMSActivity can be everything that comes to your mind, like a little MusicPlayer, a SMS-Storage-App, a... EVERYTHING you can imagine

Im using this code for the SMS-Compression-App i'll publicize when its ready

:) So thats it. I hope I could help you. 

Regards,
plusminus






