unstable results SMS notification with BroadcastReceiver

Put your problem here if it does not fit any of the other categories.

unstable results SMS notification with BroadcastReceiver

Postby shenwt » Tue Nov 04, 2008 9:48 pm

Hello everyone,

I am starting with Android and facing some little problems with coding my application.
The idea is simple : the device receives SMS, my application filter them and do some action upon such or such type of SMS.

Thanks to your forum, I found how to do so, and I can listen for incoming SMS and perform action when such or such type of SMS arrives (in my application, this is SMS from an hard coded telephone number).

The goal of this piece of code is to retrieve the very last SMS that has been received. In order to work around, and try how Android works, I just wanna count the number of SMS in inbox.

The matter with my code (you'll see it below) is that the result is not stable : when it is more than 1 call to this code, the number of SMS is not accurate anymore.

As an example, let's say that my device doesn't contain ANY SMS, and that all the SMS the device receives are from the number on which I decide whether or not there is action to perform.
1 - first SMS received : it shows there is 1 sms in box (OK)
2 - second SMS received : it shows there is 1 sms in box (NOK, should be 2)
3 - third SMS received : it shows there are 2 sms in box (NOK, should be 3, but at least increased from last step)
.. and so on.

IN THE DDMS PANEL (with Eclipse), if I press Debug on the process of my application, then the counter becomes good. So from my previous example, let's say after 3 I pressed the debug button, I would have:
4 - fourth SMS received : it shows there are 4 sms in box (OK, went back to good counting)
5 - fifth SMS received : it shows there are 5 sms in box (OK)
... and so on.

Please can you give me your opinion on where there is a pb in my code ??

Thanks so much for you help !!!

JAVA CODE :
Code: Select all

package com.degetel.mobilelocator.respappli;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.gsm.SmsMessage;


public class SMSListener extends BroadcastReceiver implements Runnable {

   private Context _ctxt;
   private Intent _intent;
   private Thread t = new Thread(this);


   @Override
   public void onReceive(Context ctxt, Intent intent) {
      _ctxt = ctxt;
      _intent = intent;
      t.start();
   }

   public void run() {
      // we need to check if the sender is the server
      Bundle bundle = _intent.getExtras();
      if (bundle != null) {
         /* Get all messages contained in the Intent */
         SmsMessage[] messages = Telephony.Sms.Intents
               .getMessagesFromIntent(_intent);

         /* retrieve the message */
         for (SmsMessage currentMessage : messages) {
            if ((currentMessage.getDisplayOriginatingAddress())

            .equalsIgnoreCase("01101981")) {

               Cursor cursor = _ctxt.getContentResolver().query(
                     Telephony.Sms.CONTENT_URI, null, null, null, "_ID");
               System.out.println("on a " + cursor.getCount()
                     + " message(s)");

               }
         }
      }
   }

}



XML code
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="mobilelocator.respappli">
   <uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.WRITE_SMS" />
   <uses-permission android:name="android.permission.READ_SMS" />
   <application android:icon="@drawable/icon" android:label="@string/app_name">
      <activity android:name=".MobileLocatorAppli" android:label="@string/app_name">
      </activity>
      <receiver android:name="SMSListener">
         <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
         </intent-filter>
      </receiver>
   </application>
</manifest>
shenwt
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 04, 2008 9:19 pm

Top

Postby Quetzalcoatl » Wed Nov 05, 2008 6:02 pm

Which version of the SDK are you using? I've got 1.0 and ' Telephony.Sms.CONTENT_URI' doesn't seem to be supported. I found a different way of doing what you're trying to do but I've not had time to test it yet before posting. I'll try and give it a quick test later on :wink: :wink:
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby shenwt » Thu Nov 06, 2008 11:56 am

Hi Quetzalcoatl,

I'm using the SDK 0.9 beta with Eclipse. Maybe that is the reason it can't be resolved.

Actually that all sounds really unclear to me: in the previous versions, it seems the packages for SMS work were different than in SDK 1. In my version, the URI is actually "content://sms
Contrary to other classes, the JavaDoc doesn't appear. However to compiles and run with no pb.

If it's only an issue of package, then how to retrieve the content URI for SMS ? In the Java doc on Google website there is nothing about that...

Thanks for helping me ;)
shenwt
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 04, 2008 9:19 pm

Postby Quetzalcoatl » Thu Nov 06, 2008 12:14 pm

Hi,

This is what I picked up from here an other forums:

Code: Select all
Cursor c = context.getContentResolver().query(Uri.parse("content://sms/inbox/"), null,null, null, null);
int num = c.getCount();


It seemed to give me the correct number of messages when I ran it once but I have not tried it by sending new messages (which is what you're actually trying to solve).
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby shenwt » Thu Nov 06, 2008 2:04 pm

Yes, I have tried this URI as well, they seem to behave the same for me...

What is the result for you when you try more than once to send an sms ??

Thx :)

Pierre
shenwt
Freshman
Freshman
 
Posts: 3
Joined: Tue Nov 04, 2008 9:19 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests