Recognize/React on incoming SMS

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Recognize/React on incoming SMS

Postby plusminus » Sun Dec 16, 2007 11:24 pm

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.

:idea: Designed/Tested with sdk-version: m5-rc14

:?: Problems/Questions: Write it right below...

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)
Image


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:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <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:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.    package="org.anddev.android.smsexample">
  4.     <uses-permission android:name="android.permission.RECEIVE_SMS" />
  5.     <application android:icon="@drawable/icon">
  6.         <!-- The Main Activity that gets started by the IntentReceiver listed below -->
  7.         <activity android:name=".SMSActivity" android:label="@string/app_name">
  8.             <intent-filter>
  9.                 <action android:name="android.intent.action.MAIN" />
  10.                 <category android:name="android.intent.category.LAUNCHER" />
  11.             </intent-filter>
  12.         </activity>
  13.         <!-- This class will react on the SMS show a notification
  14.                        and start the Main-App afterwards -->
  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. </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:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.smsexample;
  2.  
  3. import android.app.NotificationManager;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentReceiver;
  7. import android.os.Bundle;
  8. import android.provider.Telephony;
  9. import android.telephony.gsm.SmsMessage;
  10. import android.util.Log;
  11.  
  12. public class SMSReceiver extends IntentReceiver {
  13.         /** TAG used for Debug-Logging */
  14.         private static final String LOG_TAG = "SMSReceiver";
  15.  
  16.         /** The Action fired by the Android-System when a SMS was received.
  17.          * We are using the Default Package-Visibility */
  18.         private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
Parsed in 0.033 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 :idea:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         // @Override
  2.         public void onReceiveIntent(Context context, Intent intent) {
  3.                 if (intent.getAction().equals(ACTION)) {
  4.                         // if(message starts with SMStretcher recognize BYTE)
  5.                         StringBuilder sb = new StringBuilder();
  6.                        
  7.                         /* The SMS-Messages are 'hiding' within the extras of the Intent. */
  8.                         Bundle bundle = intent.getExtras();
  9.                         if (bundle != null) {
  10.                                 /* Get all messages contained in the Intent*/
  11.                                 SmsMessage[] messages =
  12.                                         Telephony.Sms.Intents.getMessagesFromIntent(intent);
  13.                                
  14.                                 /* Feed the StringBuilder with all Messages found. */
  15.                                 for (SmsMessage currentMessage : messages){
  16.                                         sb.append("Received compressed SMSnFrom: ");
  17.                                         /* Sender-Number */
  18.                                         sb.append(currentMessage.getDisplayOriginatingAddress());
  19.                                         sb.append("n----Message----n");
  20.                                         /* Actual Message-Content */
  21.                                         sb.append(currentMessage.getDisplayMessageBody());
  22.                                 }
  23.                         }
  24.                         /* Logger Debug-Output */
  25.                         Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
  26.  
  27.                         /* Show the Notification containing the Message. */
  28.                         Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Finally we start the Main-Activity doing the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         /* Start the Main-Activity */
  2.                         Intent i = new Intent(context, SMSActivity.class);
  3.                         i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  4.                         context.startActivity(i);
  5.                 }
  6.         }
  7. }
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
Last edited by plusminus on Fri Oct 03, 2008 8:11 pm, edited 6 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby kadair » Mon Dec 17, 2007 1:02 am

This is exactly what I was looking for--it works great!

Thanks again,
Ken
Last edited by kadair on Tue Dec 18, 2007 5:44 pm, edited 1 time in total.
"Your imagination is your preview of life's coming attractions." - Einstein

http://www.AppSocial.com
kadair
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Dec 16, 2007 11:02 pm
Location: Cincinnati OH, United States

Postby tum0rc0re » Mon Dec 17, 2007 6:48 am

Thanks, buddy :) It's excellent tutorial :)
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby Lex » Mon Dec 17, 2007 10:16 pm

Thank you ! Keep up the good work !
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Postby venkat » Tue Dec 18, 2007 5:09 am

Dear Plusminus, Nice work you have done.
i copied your code and i have created one activity which displays "SMSRecived".
when i run my program it is showing one error activity not defined by the manifest, also i have attached screen shot of the error and aslo i have attached my full coding, take a look.

Thanks and regards,
venkat
Attachments
SMSReceiver.rar
(33.58 KiB) Downloaded 1429 times
androidError.png
androidError.png (12.35 KiB) Viewed 157350 times
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby kadair » Tue Dec 18, 2007 5:16 am

Hi Venkat,

Would you mind posting your Android manifest file? For some reason I'm having issues downloading the source. Without looking at it, I would suggest double checking the packages in the manifest and if everything looks okay there, you may just need to delete your run configurations for that project. If you're not sure how to do that let me know and I can post a quick example.

I'll also try to download the source again so that I can take a closer look.

Regards,
Ken
Last edited by kadair on Tue Dec 18, 2007 5:44 pm, edited 1 time in total.
"Your imagination is your preview of life's coming attractions." - Einstein

http://www.AppSocial.com
kadair
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Dec 16, 2007 11:02 pm
Location: Cincinnati OH, United States

Top

Postby venkat » Tue Dec 18, 2007 5:33 am

Hi kadair :)
Thanks for ur reply, i don't how to delete run configuration for that project. i hava attached mysource once again. try to download new one.

regads,
venkat
Attachments
SMSReceiver.rar
(33.58 KiB) Downloaded 2358 times
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby kadair » Tue Dec 18, 2007 5:48 am

Venkat,

I was able to download and run your source this time. Go ahead and right-click on your project (SMSReceiver) in Eclipse. Select "Run As...", then the "Open Run Dialog..." option. You should see a pop-up that says "Create, manage, and run configurations" at the top. You need to select all of the projects under "Android Application" in the left menu and then click on the red "X" above. It will delete all of your "run configurations" for all of your Android projects. I would post some images if I had more time. Please feel free to shoot me a private message if you need further assistance. Hopefully, this will take care of you though.

Regards,
Ken
Last edited by kadair on Tue Dec 18, 2007 5:44 pm, edited 1 time in total.
"Your imagination is your preview of life's coming attractions." - Einstein

http://www.AppSocial.com
kadair
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Dec 16, 2007 11:02 pm
Location: Cincinnati OH, United States

Postby venkat » Tue Dec 18, 2007 5:53 am

Hi kadair,
now it works fine. thanks for ur help. :) Thank you very much once again.


Regads,
venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

How about sending Sms!!

Postby Nitinkcv » Sat Dec 22, 2007 7:13 pm

Hi all,

Was just wondering whether the updated SDK allows us to send out SMS's.

Thanks,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Postby plusminus » Sun Dec 23, 2007 2:55 pm

Hello Nitinkcv,

basically it is possible. There is a :src: SMSManager providing the following function:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  public void sendTextMessage(String destinationAddress, String scAddress, String text, Intent sentIntent, Intent deliveryIntent, Intent failedIntent)
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


But how to send an SMS without a GSM-Connection ;)
The Emulator probably simulates a success or a failure ... haven't tried it yet. So I cannot say if this funcitonality is already full impelmented.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: Recognize/React on incoming SMS

Postby mjpan » Fri Dec 28, 2007 7:59 am

thanks for the great tutorial.

one question, though. right now you simply use a NotificationManager to display the incoming text message. How can the message be passed to the Activity :?:

currently i have an activity, which is put onPause(), and using the IntentReceiver, is resumed through onResume(). the Activity's mIntent, however, has null as the extras bundle, despite my having explicitly put it into the extras via intent.putExtras(bundle). perhaps because the activity was on pause, its mIntent is the one that originally created it, and not the one that resumed it :?:
mjpan
Freshman
Freshman
 
Posts: 4
Joined: Mon Dec 24, 2007 2:30 am

Re: Recognize/React on incoming SMS

Postby plusminus » Fri Dec 28, 2007 1:52 pm

Hello mjpan,

mjpan wrote:Perhaps because the activity was on pause, its mIntent is the one that originally created it, and not the one that resumed it :?:


Don't think so, because Intents "die" after being used.

What do you mean with "mIntent" :?: == this.getIntent() :?:

Try putting some invented Category to the Intent and check if it arrives at the other side of the 'tunnel'. Like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         i.addCategory("MY_CATEGORY");
  2.  
  3.  
  4.  
  5.         // And on the other side of the road:
  6.  
  7.         for(String s : this.getIntent().getCategories())
  8.  
  9.                 Log.d("CATEGORY_TAG", "CAT= " + s);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: Recognize/React on incoming SMS

Postby mjpan » Fri Dec 28, 2007 6:06 pm

Hi plusminus,
Thanks for getting back to me. Yes, by mIntent, i mean this.getIntent(), where "this" is the Activity being resumed. I'm using the debugger to inspect the objects, and "mIntent" is the name of the member field of the Activity object. I have also verified (using the debugger) that if my application is NOT running, then mIntent.getExtras() is the one created in SMSReceiver, while if application is already running, then mIntent.getExtras() is null. Do you know how to open a ticket against this bug in Android?
Thx
mjpan

plusminus wrote:Hello mjpan,

What do you mean with "mIntent" :?: == this.getIntent() :?:

Try putting some invented Category to the Intent and check if it arrives at the other side of the 'tunnel'. Like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         i.addCategory("MY_CATEGORY");
  2.  
  3.         // And on the other side of the road:
  4.         for(String s : this.getIntent().getCategories())
  5.                 Log.d("CATEGORY_TAG", "CAT= " + s);
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
mjpan
Freshman
Freshman
 
Posts: 4
Joined: Mon Dec 24, 2007 2:30 am

Postby plusminus » Fri Dec 28, 2007 6:10 pm

Hello mjpan,

did you try the Category-Thing :?:

There is no official Bug-Tracking-System yet, but on my Bugs posting them to the GoogleGroups and hoping a moderator would see them was fine ^^.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests