Recognize/React on incoming SMS

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

Problems recieving CDMA SMSes

Postby saurabhnigam » Fri Aug 20, 2010 8:06 am

Hi ,
Thanks for the wonderful post .Well I followed your method it works well on GSM
but I faced issues in Recieving CDMA SMS Messages.
Please Help.
Saurabh
saurabhnigam
Freshman
Freshman
 
Posts: 2
Joined: Fri Aug 20, 2010 7:52 am

Top

Re: Recognize/React on incoming SMS

Postby yusuf » Thu Aug 26, 2010 2:25 pm

Telephony.Sms.Intents.getMessagesFromIntent(intent);
Here Telephony is showing error
Androiding.........................
yusuf
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Mon Feb 15, 2010 4:25 pm
Location: Pune,India

Re: Recognize/React on incoming SMS

Postby edreamz » Fri Sep 10, 2010 9:09 pm

I just tried out this app on the 2.2 SDk, it certainly needs some changes to the code, since the SDKs have changed a lot. :)

1. SmsActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.felix.smsreact;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5.  
  6. public class SmsActivity extends Activity {
  7.  
  8.         @Override
  9.         protected void onCreate(Bundle savedInstanceState) {
  10.                 super.onCreate(savedInstanceState);
  11.                 setContentView(R.layout.main);
  12.         }
  13. }
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


2. SmsReactor.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.felix.smsreact;
  2.  
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.telephony.SmsMessage;
  8. import android.util.Log;
  9. import android.widget.Toast;
  10.  
  11. public class SmsReactor extends BroadcastReceiver {
  12.         /** TAG used for Debug-Logging */
  13.         private static final String LOG_TAG = "SMSReactor";
  14.  
  15.         /** The Action fired by the Android-System when a SMS was received.
  16.          * We are using the Default Package-Visibility */
  17.  
  18.         private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
  19.  
  20.         public void onReceive(Context context, Intent intent) {
  21.                 if (intent.getAction().equals(ACTION)) {
  22.                         // if(message starts with SMStretcher recognize BYTE)
  23.                         StringBuilder sb = new StringBuilder();
  24.  
  25.                         /* The SMS-Messages are 'hiding' within the extras of the Intent. */
  26.                         Bundle bundle = intent.getExtras();
  27.                         if (bundle != null) {
  28.                                 /* Get all messages contained in the Intent
  29.                                  * Telephony.Sms.Intents.getMessagesFromIntent(intent) does not work anymore
  30.                                  * hence the below changes
  31.                                  */
  32.                                 Object[] pduObj = (Object[]) bundle.get("pdus");
  33.                                 SmsMessage[] messages = new SmsMessage[pduObj.length];
  34.                                 for(int i=0;i<pduObj.length;i++)
  35.                                         messages[i]=SmsMessage.createFromPdu((byte[])pduObj[i]);
  36.                                 /* Feed the StringBuilder with all Messages found. */
  37.                                 for (SmsMessage currentMessage : messages){
  38.                                         sb.append("SMS Received From: ");
  39.                                         /* Sender-Number */
  40.                                         sb.append(currentMessage.getDisplayOriginatingAddress());
  41.                                         sb.append("\nMessage : ");
  42.                                         /* Actual Message-Content */
  43.                                         sb.append(currentMessage.getDisplayMessageBody());
  44.                                 }
  45.                         }
  46.                         /* Logger Debug-Output */
  47.                         Log.i(LOG_TAG, "[SMSApp] onReceive: " + sb);
  48.  
  49.                         /* Show the Notification containing the Message. */
  50.                         Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
  51.                         /* Start the Main-Activity */
  52.                         Intent i = new Intent(context, SmsActivity.class);
  53.                         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  54.                         context.startActivity(i);
  55.                 }
  56.         }
  57. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


3. Manifest.xml
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.         package="com.felix.smsreact" android:versionCode="1"
  4.         android:versionName="1.0">
  5.         <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
  6.         <application android:icon="@drawable/icon" android:label="@string/app_name">
  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.                 <receiver android:name=".SmsReactor">
  14.                         <intent-filter>
  15.                                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  16.                         </intent-filter>
  17.                 </receiver>
  18.         </application>
  19. </manifest>
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
User avatar
edreamz
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Aug 06, 2010 2:29 am
Location: U.S

Re: Recognize/React on incoming SMS

Postby conandor » Sat Sep 11, 2010 4:55 pm

How can I have get outgoing and incoming sms append on the same text? somesort like chatting
conandor
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Sep 11, 2010 4:53 pm

Re: Recognize/React on incoming SMS

Postby spring » Tue Sep 14, 2010 3:23 am

good thing,thank for you sharing :D
spring
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Jan 09, 2010 4:37 am
Location: China

Re: Recognize/React on incoming SMS

Postby peru0002 » Tue Oct 05, 2010 7:27 am

Hi,

Any idea how to delete the incoming message after displaying the toast? I can't use the getContentResolver() method because I am trying to do so from BroadcastReceiver and it is not an Activity.

Any way to work around this?

Thanks In Advance,
Perumal
peru0002
Developer
Developer
 
Posts: 45
Joined: Thu Jan 21, 2010 5:56 am

Top

Re: Recognize/React on incoming SMS

Postby farhanahmad123 » Tue Oct 12, 2010 2:22 pm

Hi,
I ve been searching for some example code which can analyse incoming sms and then delete it.... could any body help me... i can analyse incoming sms but cant delete it.... sort of new in android... plz help
regards,
farhanahmad123
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Oct 12, 2010 2:15 pm

Re: Recognize/React on incoming SMS

Postby Dliswa » Tue Oct 19, 2010 2:48 pm

dear plusminus
when i import package i.e

import android.content.IntentReceiver;
Elcipse give me errror like Can not resolve.
by the way where i can find IntentReceiver.

Waiting for Your Replay.
Dliswa
Developer
Developer
 
Posts: 30
Joined: Fri Oct 15, 2010 5:56 am

Re: Recognize/React on incoming SMS

Postby Dliswa » Tue Oct 19, 2010 3:16 pm

hi edreamz
Great Work
Dliswa
Developer
Developer
 
Posts: 30
Joined: Fri Oct 15, 2010 5:56 am

Re: Recognize/React on incoming SMS

Postby Dliswa » Tue Oct 19, 2010 8:55 pm

Hey Everyone How can we send SMS from Emulator A to Emulator B such that when Emulator B receive a Text message (After the Permission is Granted to emulator B) and gets its GPS Coordinates and send it As a SMS in Respond to Emulator A.
Dliswa
Developer
Developer
 
Posts: 30
Joined: Fri Oct 15, 2010 5:56 am

Re: Recognize/React on incoming SMS

Postby ashwini.dabir » Tue Dec 21, 2010 10:28 am

Hi PlusMinus,

Thanx for this tutorial.. it was of great help .. :)
Could u please help me out on 1 more thing .. once i capture the incoming sms, i dont want the sms to reach the inbox.
I will save it in a file. Pl help me how to prevent the sms from reaching the inbox.

Thanx in advance,
Ash.
ashwini.dabir
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 20, 2010 10:45 am

Re: Recognize/React on incoming SMS

Postby IrishGrumPy » Thu Feb 03, 2011 7:09 pm

Hi guys, can anyone advise what code needs to be added to this app that would automatically send a reply to the message sender saying "i got your message - busy at the moment - will ring ya later" or whatever?

If anyone can advise, it would be great!!! :)
IrishGrumPy
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Feb 03, 2011 7:07 pm

Re: Recognize/React on incoming SMS

Postby DMacATTACK » Sat Feb 05, 2011 11:30 pm

IrishGrumPy wrote:Hi guys, can anyone advise what code needs to be added to this app that would automatically send a reply to the message sender saying "i got your message - busy at the moment - will ring ya later" or whatever?

If anyone can advise, it would be great!!! :)


I can't advise but I can advertise!
You can check out my app:
SMS Answering Machine
https://market.android.com/details?id=com.dmacattack.SmsAnsweringMachine

or

SMS Answering Machine Free
https://market.android.com/details?id=com.dmacattack.SmsAnsweringMachineFree
User avatar
DMacATTACK
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Thu Dec 31, 2009 6:31 pm
Location: Canada

Re: Recognize/React on incoming SMS

Postby theblitz » Tue Oct 11, 2011 2:45 pm

Small change.
Need to replace:
Code: Select all
import android.telephony.SmsMessage;

with:
Code: Select all
import android.telephony.gsm.SmsMessage;


edreamz wrote:I just tried out this app on the 2.2 SDk, it certainly needs some changes to the code, since the SDKs have changed a lot. :)

2. SmsReactor.java
Code: Select all
package com.felix.smsreact;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsReactor extends BroadcastReceiver {
   /** TAG used for Debug-Logging */
   private static final String LOG_TAG = "SMSReactor";

   /** 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";

   public void onReceive(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
             * Telephony.Sms.Intents.getMessagesFromIntent(intent) does not work anymore
             * hence the below changes
             */
            Object[] pduObj = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pduObj.length];
            for(int i=0;i<pduObj.length;i++)
               messages[i]=SmsMessage.createFromPdu((byte[])pduObj[i]);
            /* Feed the StringBuilder with all Messages found. */
            for (SmsMessage currentMessage : messages){
               sb.append("SMS Received From: ");
               /* Sender-Number */
               sb.append(currentMessage.getDisplayOriginatingAddress());
               sb.append("\nMessage : ");
               /* Actual Message-Content */
               sb.append(currentMessage.getDisplayMessageBody());
            }
         }
         /* Logger Debug-Output */
         Log.i(LOG_TAG, "[SMSApp] onReceive: " + sb);

         /* Show the Notification containing the Message. */
         Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
         /* Start the Main-Activity */
         Intent i = new Intent(context, SmsActivity.class);
         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(i);
      }
   }
}

theblitz
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 18, 2011 9:11 am

Re: Recognize/React on incoming SMS

Postby Dominator » Mon Jan 09, 2012 11:05 pm

Hello guys,

Im totally new to Android software developing so i would appreciate very very much if someone could help me on this:

This application is working nicely on my phone,
and now i would like to add some MAIN ACTIVITY in SmsActivity.java class,
i want to send, by email, the SMS that has been received, to some email adress,
so i added this code

Code: Select all
                        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
         
         /* Start the Main-Activity */

         
         
           Intent i = new Intent(Intent.ACTION_SEND);
           i.setType("message/rfc822");
           i.putExtra(Intent.EXTRA_EMAIL  , new String[]{email adress});
           i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
           i.putExtra(Intent.EXTRA_TEXT   , text);

           try {
              context.startActivity(Intent.createChooser(i, "Send mail..."));

           } catch (android.content.ActivityNotFoundException ex) {
               Toast.makeText(context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
           }


but it's not working. I't cannot be tested on emulator, so im sending sms over phone, and application simply stops (''application is forced to close..''), so i really dont' know where the problem could be?!
Dominator
Freshman
Freshman
 
Posts: 4
Joined: Mon Jan 09, 2012 10:56 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests