Show me the code! – By Davanum Srinivas

Web Services, Apache, Websphere, IBM, etc.

Android – Listen for incoming SMS messages

with 19 comments

Get the latest m3-rc37a version of android

Here’s a screen shot

1

Setup the Android Manifest with the permission and the Intent Receiver


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.apache.sms">
    <uses-permission id="android.permission.RECEIVE_SMS" />
    <application>
        <receiver class="SMSApp">
            <intent-filter>
                <action android:value="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
</manifest> 

Code a simple Intent Listener


package org.apache.sms;

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.util.Log;
import android.telephony.gsm.SmsMessage;

public class SMSApp extends IntentReceiver {
    private static final String LOG_TAG = "SMSApp";

    /* package */ static final String ACTION =
            "android.provider.Telephony.SMS_RECEIVED";

    public void onReceiveIntent(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION)) {
            StringBuilder buf = new StringBuilder();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
                for (int i = 0; i &lt; messages.length; i++) {
                    SmsMessage message = messages[i];
                    buf.append("Received SMS from  ");
                    buf.append(message.getDisplayOriginatingAddress());
                    buf.append(" - ");
                    buf.append(message.getDisplayMessageBody());
                }
            }
            Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + buf);
            NotificationManager nm = (NotificationManager) context.getSystemService(
                    Context.NOTIFICATION_SERVICE);

            nm.notifyWithText(123, buf.toString(),
                    NotificationManager.LENGTH_LONG, null);

        }
    }

    private void appendData(StringBuilder buf, String key, String value) {
        buf.append(", ");
        buf.append(key);
        buf.append('=');
        buf.append(value);
    }
}

Running the sample



  • Install the APK using “adb install SMSApp.apk” as usual

  • Open a telnet session to localhost at port 5554 and type “sms send +5085551212 hello” to simulate an sms message
  • Download Source and APK from here – SMSApp.zip

Written by Davanum Srinivas

December 15, 2007 at 10:06 am

Posted in Uncategorized

19 Responses

Subscribe to comments with RSS.

  1. Dims – you need full text Atom feeds! :-)

    Dan Diephouse

    December 15, 2007 at 3:01 pm

  2. good job, D.

    andrewxuheng

    December 20, 2007 at 1:32 pm

  3. Very useful and good written tutorial. Thank you!

    Russian Translit

    January 10, 2008 at 8:57 pm

  4. Hi, davanum nice tutorial…. i tried it and it sun successfully….however I was wondering if sending the sms as part of an activity is possible or not ? I was trying to use something called SMSManager , but couldn’t test it.Would be greatful if you could suggest something.
    thanks in avance:]

    csagar

    January 13, 2008 at 8:53 am

  5. Keep up the good work. I need all the help I need and youttutorial have gone a long way to close the gaps. Thanks :)

    Joseph Dung

    February 12, 2008 at 7:57 am

  6. Hi,
    This is cool. But I thought that the emulator should be listening to any incoming SMSs by default. Its fucntionality that should be provided in the phone.

    Anyway, this is a good example.

    Dexter

    February 19, 2008 at 2:31 pm

  7. Hi this is bhavan am just tried to use your SMSApp application but i got this error at line
    nm.notifyWithText(123, buf.toString(),
    NotificationManager.LENGTH_LONG, null);
    NotificationManager.LENGTH_LONG cannot be resolved
    could you please suggest how to solve this error to my mail id.And is this application work to send an sms from emulator?

    Bhavana

    March 20, 2008 at 4:02 am

  8. i’ve the same error :|

    NotificationManager.LENGTH_LONG cannot be resolved

    shitfull

    March 28, 2008 at 1:01 pm

  9. You can use Toast.maketext() replace Notification manager

    RoSippA

    May 6, 2008 at 3:03 am

  10. Hello,
    Use

    import android.widget.Toast; // import

    Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
    instead of NotificationManager.

    Rifat Shahriyar

    May 9, 2008 at 4:55 am

  11. sorry buf.toString() instead of sb.toString()

    Rifat Shahriyar

    May 9, 2008 at 4:57 am

  12. .i’m using m5-rc15 and have used android.widget.Toast as Rafit says w/o error. But I’m having a problem deploying: when i try “adb install “, 0 bytes install – “110 KB/s (0 bytes in 3520.000s)”. In Eclipse, I’m not sure how to setup the prj so that it builds and runs in the emulator (theres no activity in this prj)..
    Thoughts?

    ben

    June 6, 2008 at 9:42 pm

  13. This does not work anymmore..

    sylvester steele

    July 2, 2008 at 6:49 am

  14. Hey Davanum,

    where is SMSApp.apk. this zip file cannot be founded..

    janaarth

    August 18, 2008 at 5:03 am

  15. Hi Davanum,

    Greetings to u.

    I’m newer to Android.

    I’m trying to get depth knowledge in Intent, IntentReceiver, Service..

    I’ve tried ur sample application. I got error in the part “Code a simple Intent Listener” as “The import android.provider.Telephony cannot be resolved”.

    Can u please help me as soon as possible… :(

    Thanks in advance.

    Yasmin

    October 17, 2008 at 2:24 am

  16. the class IntentReceiver does no longer exists in the SDK 1.1 and is renamed to BroadcastReceiver.

    hope this helps running this nice example.

    guido

    February 22, 2009 at 3:39 pm

  17. Hi,everyone..
    I have a problem to this code..
    I have an error on
    import android.provider.Telephony;

    the import cannot be risolved..
    Why?

    DoM

    March 16, 2009 at 5:30 am

  18. DoM:

    It appears this package is only available in the Open Source version and not available in 1.1 SDK.

    MV

    March 18, 2009 at 10:34 pm

  19. Nice tutorial

    Did somebody updated this tutorial to 1.1 SDK.

    Also the books available are not updated.

    JHandal

    March 25, 2009 at 9:14 pm


Leave a Reply