sanjana
sanjana

Reputation: 641

content observer called twice for receiving sms

Hi I am developing an android SMS app where in I am using the ContentObserver to know the incoming message similar to this link

http://rdcworld-android.blogspot.in/2011/10/listen-sms-mms-programmatically-android.html

I need to get the count of received SMS. But, the ContentObserver onChange method is called twice and I am not able to get the proper count of received SMS. How do I resolve this

Please Help.Thanks!

Upvotes: 1

Views: 1524

Answers (1)

Di Vero Labs
Di Vero Labs

Reputation: 344

It has something to do with when the SMS comes in, it'll fire, and then again when it syncs with the underlying database(s). Best solution I've found, implement a way to ignore the second call:

Long theDT = System.currentTimeMillis();
Long nextAM = Long.valueOf(1000);  //Use a 1 second minimum delay to avoid repeated calls
Long lastAM = preferences.getLong("lastAM", 0);
if ((lastAM + nextAM) < theDT1){
    SharedPreferences.Editor editor = preferences.edit();
    editor.putLong("lastAM", theDT); // value to store
    editor.commit();

    // DO WHAT YOU NEED TO DO HERE
}

Upvotes: 3

Related Questions