Recognize/React on incoming SMS

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

Re: Recognize/React on incoming SMS: HELP

Postby bjreddi » Fri Apr 25, 2008 12:07 pm

Plusminus,

Thank you very much in tutoring us with very good applications.

I have tried the example that u have posted. "Rect on incoming SMS"

I have two Java classes SMSActivity.java, SMSReceiver.Java. I just copied and ur code and ran the application.

I got this note when i ran the application:

"Helloworld, SMSActivity"

I didnt get any SMS received notification.

I did the way u mentioned in the tutorial.

I have no errors at all. But how to get the Notification. Please help me.

Regards,
Jyothi
bjreddi
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 03, 2008 1:24 pm

Top

Postby kiran » Fri Apr 25, 2008 12:18 pm

Hi,
use the emulator control in ddms to send a SMS message to your emulator. The message will be displayed automatically.
Regards,
Kiran.
:) smile it is the key that fits the lock of everybody's heart
User avatar
kiran
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Thu Nov 22, 2007 11:36 am
Location: India

Postby bjreddi » Fri Apr 25, 2008 12:21 pm

Hi Kiran,

I am just a beginner, I really dont know how to use this DDMS from Emulator,.

Could please guide me.

Thanks n Regards,
Jyothi
bjreddi
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 03, 2008 1:24 pm

Postby kiran » Fri Apr 25, 2008 12:41 pm

Hi,
If you are using eclipse with ADT plugin then you should have DDMS perspective in your eclipse. On Opening DDMS perspective, to the left of the screen you will have the Emulator control view along with processes. You can also run ddms.bat file in tools folder.
Regards,
Kiran.
:) smile it is the key that fits the lock of everybody's heart
User avatar
kiran
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Thu Nov 22, 2007 11:36 am
Location: India

Postby kiran » Fri Apr 25, 2008 12:51 pm

Hi,
This link shows how to open DDMS in eclipse and use file browser. http://www.anddev.org/tinytut_-_pull-pu ... -t113.html
Regards,
Kiran
:) smile it is the key that fits the lock of everybody's heart
User avatar
kiran
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Thu Nov 22, 2007 11:36 am
Location: India

Postby bjreddi » Fri Apr 25, 2008 12:58 pm

Hi Kiran,

Thank you so much.

Yes, I checked the Push and Pull from/to files to emulator.
And this time i clicked on Devices,, where i was able to my application and click on it. and again clicked on Emulator controler then it enables the all fields.

Great!! thanks once more,.

Regards,
Jyothi
bjreddi
Junior Developer
Junior Developer
 
Posts: 17
Joined: Thu Apr 03, 2008 1:24 pm

Top

Postby dkkundudolan » Sun Apr 27, 2008 7:40 am

Hi Sebi,
I have a problem that , when I am sending request to the server, server is sending reply to me with all the details. i can see the reply at that time itself when server is sending to me. But i want to see the message whenever i want.
please , can you tell me how can i see the incoming message from server at any time?
is there any related code with that? plz reply me fast. I had gone through the following code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2.  
  3. import android.app.Notification;
  4.  
  5. import android.app.NotificationManager;
  6.  
  7. import android.content.Context;
  8.  
  9. import android.content.Intent;
  10.  
  11. import android.content.Resources;
  12.  
  13.  
  14.  
  15. import android.os.Bundle;
  16.  
  17.  
  18.  
  19. import android.os.Message;
  20.  
  21. ;
  22.  
  23. import android.provider.Telephony.Sms;
  24.  
  25. import android.view.View;
  26.  
  27. import android.view.ViewInflate;
  28.  
  29. import android.widget.Button;
  30.  
  31. import android.widget.TextView;
  32.  
  33. import android.widget.Toast;
  34.  
  35.  
  36.  
  37. public class MatchingStuff extends Activity{
  38.  
  39.    // private final Toast mToast;
  40.  
  41.     private String textMessage="";
  42.  
  43.     private TextView Text;
  44.  
  45.     private Mstuff mstuff;
  46.  
  47.    // private final Toast t;
  48.  
  49.     private ViewInflate inflate = null;
  50.  
  51.    
  52.  
  53.     @Override
  54.  
  55.      public void onCreate(Bundle icicle){
  56.  
  57.        super.onCreate(icicle);
  58.  
  59.        setContentView(R.layout.matchingstuff);
  60.  
  61.      
  62.  
  63.     TextView serverreptext = (TextView) findViewById(R.id.serverrep);
  64.  
  65.     Bundle bundle = this.getIntent().getExtras();
  66.  
  67.     String s = bundle.getString("ServerReply");
  68.  
  69.     serverreptext.setText(s);
  70.  
  71.     Button button = (Button) findViewById(R.id.notify);
  72.  
  73.     button.setOnClickListener(new Button.OnClickListener() {
  74.  
  75.  
  76.  
  77.             public void onClick(View arg0) {
  78.  
  79.                 showToast();
  80.  
  81.                 showNotification(mstuff);
  82.  
  83.             }
  84.  
  85.            private void showNotification(Mstuff mstuff) {
  86.  
  87.                 }
  88.  
  89.            private void showToast() {
  90.  
  91.                 View view = inflateView(R.layout.matchingstuff_message);
  92.  
  93.        TextView tv = (TextView)view.findViewById(R.id.message);
  94.  
  95.         tv.setText("HttpClient");
  96.  
  97.         Toast t = new Toast(mstuff);
  98.  
  99.         t.setView(view);
  100.  
  101.         t.setDuration(Toast.LENGTH_LONG);
  102.  
  103.         t.show();
  104.  
  105.                  }
  106.  
  107.             private View inflateView(int matchingstuff) {
  108.  
  109.                 ViewInflate v = (ViewInflate)getSystemService(Context.INFLATE_SERVICE);
  110.  
  111.                 return v.inflate(R.layout.catalog, null, null);
  112.  
  113.             }
  114.  
  115.  
  116.  
  117.             private void showNotification() {
  118.  
  119.                 NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
  120.  
  121.                  String from = "Match found";
  122.  
  123.        
  124.  
  125.                 Intent contentIntent = new Intent(MatchingStuff.this, Settings.class);
  126.  
  127.                 // Intent contentIntent  = new Intent();
  128.  
  129.                 String text = getString(R.string.matchingstuff_text);
  130.  
  131.                 Intent appIntent = new Intent(Intent.VIEW_ACTION, Sms.Inbox.CONTENT_URI);
  132.  
  133.                 //Intent appIntent = new Intent();
  134.  
  135.                
  136.  
  137.                 Notification notif = new Notification(
  138.  
  139.                    MatchingStuff.this,                      
  140.  
  141.                    R.drawable.stat_sample,    
  142.  
  143.                    text,                  
  144.  
  145.                    System.currentTimeMillis(),  
  146.  
  147.                    from,                      
  148.  
  149.                    textMessage,                    
  150.  
  151.                    contentIntent,              
  152.  
  153.                    R.drawable.shadow,
  154.  
  155.                    getText(R.string.activity_sample_code),
  156.  
  157.                    appIntent);
  158.  
  159.              nm.notify(R.string.matchingstuff_text, notif);
  160.  
  161.             }
  162.  
  163.      });
  164.  
  165.     }
  166.  
  167. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
dkkundudolan
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Mon Mar 03, 2008 12:50 pm

Postby kingkung » Mon Jul 07, 2008 6:04 pm

Thanks for the help, the SMS receiver works great.

How do I create this IntentReceiver within an activity (instead of in the manifest)? My problem is that there is no android.provider.Telephony.SMS_RECEIVED value accessible in code.

Thanks,

James
kingkung
Freshman
Freshman
 
Posts: 9
Joined: Wed Apr 16, 2008 6:24 pm

Show SMS in native SMS/ Message application

Postby faiz.india » Mon Sep 15, 2008 11:55 am

Hi All,
On Receipt of incoming message, i want to show that message in the inbuilt native messageing application. Is there any provision to do so. If yes please let me know
faiz.india
Developer
Developer
 
Posts: 32
Joined: Mon Jul 28, 2008 12:21 pm
Location: Pune - India

Re: Show SMS in native SMS/ Message application

Postby plusminus » Mon Sep 15, 2008 1:07 pm

faiz.india wrote:Hi All,
On Receipt of incoming message, i want to show that message in the inbuilt native messageing application. Is there any provision to do so. If yes please let me know


You want to open the Systems SMS app, when an SMS is received :?:
Thats what will happen in Android 1.0 I strongly assume.

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

Show SMS in native SMS/ Message application

Postby faiz.india » Mon Sep 15, 2008 1:18 pm

Hi plusminus,

Thanks for your quick response.

You are right, i want to open the Systems SMS app, when an SMS is received.

Lets see if we have it in SDK 1.0

Regards,
Faiz
faiz.india
Developer
Developer
 
Posts: 32
Joined: Mon Jul 28, 2008 12:21 pm
Location: Pune - India

Re: Show SMS in native SMS/ Message application

Postby plusminus » Mon Sep 15, 2008 7:28 pm

faiz.india wrote:Hi plusminus,

Thanks for your quick response.

You are right, i want to open the Systems SMS app, when an SMS is received.

Lets see if we have it in SDK 1.0

Regards,
Faiz


Hello faiz,

maybe this helps you a bit until 1.0:

:src: http://www.anddev.org/viewtopic.php?p=10184

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

Postby Klinge » Fri Sep 26, 2008 2:17 pm

Hi

How do you receive sms messages in the 1.0 SDK?
I found this solution in one of the google groups but it seems to be way to complicated for such a simple task.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class ServerMessagesReceiver extends BroadcastReceiver {
  3.  
  4.  
  5.  
  6.     static final String ACTION =
  7.  
  8. "android.provider.Telephony.SMS_RECEIVED";
  9.  
  10.  
  11.  
  12.     public void onReceive(Context context, Intent intent) {
  13.  
  14.         if (intent.getAction().equals(ACTION)) {
  15.  
  16.                  StringBuilder buf = new StringBuilder();
  17.  
  18.                  Bundle bundle = intent.getExtras();
  19.  
  20.                  if (bundle != null) {
  21.  
  22.                          Object[] pdusObj = (Object[]) bundle.get("pdus");
  23.  
  24.                          SmsMessage[] messages = new SmsMessage[pdusObj.length];
  25.  
  26.                          for (int i = 0; i<pdusObj.length; i++) {
  27.  
  28.                                  messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]);
  29.  
  30.                          }
  31.  
  32.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Klinge
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Sep 26, 2008 2:13 pm
Location: Germany

Postby Artur79 » Wed Oct 01, 2008 1:18 pm

I had tried to rebuild tutorial code for SDK 1.0 using Klinge' solution for retrieving messages from Bundle. But it's not working how I'd expected, here is a source:

SMSReceiver.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.anddev.android.smsexample;
  3.  
  4.  
  5.  
  6. import android.content.BroadcastReceiver;
  7.  
  8. import android.content.Context;
  9.  
  10. import android.content.Intent;
  11.  
  12. import android.os.Bundle;
  13.  
  14. import android.telephony.gsm.SmsMessage;
  15.  
  16. import android.util.Log;
  17.  
  18. import android.widget.Toast;
  19.  
  20.  
  21.  
  22. public class SMSReceiver extends BroadcastReceiver {
  23.  
  24.      /** TAG used for Debug-Logging */
  25.  
  26.      protected static final String LOG_TAG = "SMSReceiver";
  27.  
  28.  
  29.  
  30.      /** The Action fired by the Android-System when a SMS was received.
  31.  
  32.       * We are using the Default Package-Visibility */
  33.  
  34.      private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
  35.  
  36.    
  37.  
  38.  // @Override
  39.  
  40.     public void onReceive(Context context, Intent intent) {
  41.  
  42.          
  43.  
  44.         Log.i(LOG_TAG, "[inside onReceive] ");
  45.  
  46.        
  47.  
  48.         if (intent.getAction().equals(ACTION)) {
  49.  
  50.              
  51.  
  52.                  StringBuilder sb = new StringBuilder();
  53.  
  54.              Bundle bundle = intent.getExtras();
  55.  
  56.              
  57.  
  58.              if (bundle != null) {
  59.  
  60.                  Object[] pdusObj = (Object[]) bundle.get("pdus");
  61.  
  62.                  SmsMessage[] messages = new SmsMessage[pdusObj.length];
  63.  
  64.                  for (int i = 0; i<pdusObj.length; i++) {
  65.  
  66.                          messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]);
  67.  
  68.                  }
  69.  
  70.              
  71.  
  72.                  Log.i(LOG_TAG, "[SMSApp Bundle] " + bundle.toString());
  73.  
  74.              
  75.  
  76.                   // Feed the StringBuilder with all Messages found.
  77.  
  78.                       for (SmsMessage currentMessage : messages){
  79.  
  80.                             sb.append("Received compressed SMS\nFrom: ");
  81.  
  82.                             // Sender-Number
  83.  
  84.                             sb.append(currentMessage.getDisplayOriginatingAddress());
  85.  
  86.                             sb.append("\n----Message----\n");
  87.  
  88.                             // Actual Message-Content
  89.  
  90.                             sb.append(currentMessage.getDisplayMessageBody());
  91.  
  92.                       }
  93.  
  94.               }
  95.  
  96.               // Logger Debug-Output
  97.  
  98.               Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
  99.  
  100.  
  101.  
  102.               // Show the Notification containing the Message.
  103.  
  104.               Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
  105.  
  106.  
  107.  
  108.               // Consume this intent, that no other application will notice it.
  109.  
  110.               this.abortBroadcast();
  111.  
  112.              
  113.  
  114.               // Start the Main-Activity
  115.  
  116.               Intent i = new Intent(context, SMSActivity.class);
  117.  
  118.              // i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
  119.  
  120.               i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  121.  
  122.               context.startActivity(i);
  123.  
  124.              
  125.  
  126.          }
  127.  
  128.     }
  129.  
  130. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


SMSReceiver.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.anddev.android.smsexample;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.util.Log;
  11.  
  12.  
  13.  
  14. public class SMSActivity extends Activity {
  15.  
  16.    
  17.  
  18.     @Override
  19.  
  20.     public void onCreate(Bundle savedInstanceState) {
  21.  
  22.         super.onCreate(savedInstanceState);
  23.  
  24.        
  25.  
  26.         Log.v(SMSReceiver.LOG_TAG,"inside SMS Activity");
  27.  
  28.     }
  29.  
  30. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Manifest without any changes
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.      package="org.anddev.android.smsexample"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0.0">
  11.  
  12.     <uses-permission android:name="android.permission.RECEIVE_SMS" />
  13.  
  14.     <application android:icon="@drawable/icon">
  15.  
  16.         <!-- The Main Activity that gets started by the IntentReceiver listed below -->
  17.  
  18.         <activity android:name=".SMSActivity" android:label="@string/app_name">
  19.  
  20.             <intent-filter>
  21.  
  22.                 <action android:name="android.intent.action.MAIN" />
  23.  
  24.                 <category android:name="android.intent.category.LAUNCHER" />
  25.  
  26.             </intent-filter>
  27.  
  28.         </activity>
  29.  
  30.         <!-- This class will react on the SMS show a notification
  31.  
  32.                        and start the Main-App afterwards -->
  33.  
  34.         <receiver android:name=".SMSReceiver">
  35.  
  36.             <intent-filter>
  37.  
  38.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  39.  
  40.             </intent-filter>
  41.  
  42.         </receiver>    
  43.  
  44.     </application>
  45.  
  46. </manifest>
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


When I'm running a project I get a message in a log that SMSActivity has been started, what is strange cuz I've expected that it'll be started only when I'll send an SMS via Emulator.

After sending SMS via Emulator I had following return in LogCat so works but there are still some errors.
Code: Select all
10-01 12:23:29.023: INFO/SMSReceiver(1152): [inside onReceive]
10-01 12:23:29.023: INFO/SMSReceiver(1152): [SMSApp Bundle] Bundle[{pdus=[Ljava.lang.Object;@4341ed70}]
10-01 12:23:29.023: INFO/SMSReceiver(1152): [SMSApp] onReceiveIntent: Received compressed SMS
10-01 12:23:29.023: INFO/SMSReceiver(1152): From: 004343434343
10-01 12:23:29.023: INFO/SMSReceiver(1152): ----Message----
10-01 12:23:29.023: INFO/SMSReceiver(1152): another test
10-01 12:23:29.033: INFO/NotificationService(46): enqueueToast pkg=org.anddev.android.smsexample callback=android.app.ITransientNotification$Stub$Proxy@434fec78 duration=1
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152): BroadcastReceiver trying to return result during a non-ordered broadcast
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.content.BroadcastReceiver.checkSynchronousHint(BroadcastReceiver.java:388)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.content.BroadcastReceiver.abortBroadcast(BroadcastReceiver.java:340)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at org.anddev.android.smsexample.SMSReceiver.onReceive(SMSReceiver.java:55)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2280)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.app.ActivityThread.access$2700(ActivityThread.java:112)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1628)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.os.Handler.dispatchMessage(Handler.java:88)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.os.Looper.loop(Looper.java:123)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at android.app.ActivityThread.main(ActivityThread.java:3742)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at java.lang.reflect.Method.invoke(Method.java:515)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
10-01 12:23:29.043: ERROR/BroadcastReceiver(1152):     at dalvik.system.NativeStart.main(Native Method)
10-01 12:23:29.062: DEBUG/SmsProvider(85): insert url=content://sms/inbox, match=2
10-01 12:23:29.072: INFO/ActivityManager(46): Starting activity: Intent { flags=0x10000000 comp={org.anddev.android.smsexample/org.anddev.android.smsexample.SMSActivity} }
10-01 12:23:29.683: INFO/ActivityManager(46): Stopping service: com.android.mms/.transaction.SmsReceiverService

Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

Postby Snapper67 » Fri Oct 03, 2008 4:16 pm

Artur79 wrote:After sending SMS via Emulator I had following return in LogCat so works but there are still some errors.


Need to remove the line
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.abortBroadcast();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


This is a normal broadcast and can not be aborted.
Snapper67
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jul 25, 2008 5:05 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests