0

I start a service that registers ContentObserver on the SMS inbox like so

smsObserver = new SmsObserver(new Handler());
getContentResolver().registerContentObserver(Uri.parse("content://sms/inbox"), true, smsObserver);

Here is the class, I just want to know when a change is made to the provider But I dont get the callback to the onChange method

private class SmsObserver extends ContentObserver{

    public SmsObserver(Handler handler) {
        super(handler);
    }

    @Override
    public void onChange(boolean selfChange){
        super.onChange(selfChange);
        Cursor c = getContentResolver().query(Uri.parse("content://sms/inbox"),
                new String[] {"thread_id"},"address" + "=" + from,null,null);
        if(c != null && c.moveToLast()){
            long id = c.getLong(0);
            Log.d("ID", c.getString(0));
        }
        c.close();
    }
}

I also register an observer for the Uri content://mms-sms/conversations and that one works and I just copied how I did everything from this one over to the SmsObserver class so I dont know whats wrong, can I not set an observer on the content://sms/inbox?

Nix
  • 57,072
  • 29
  • 149
  • 198
tyczj
  • 71,600
  • 54
  • 194
  • 296

3 Answers3

1

-First, register your ContentObserver like this:

getContentResolver().registerContentObserver(Uri.parse("content://mms-sms/inbox"), true, smsObserver);

-Then, override the following method in your ContentObserver:

@Override    
public void onChange(boolean selfChange, Uri uri){}
nifo
  • 593
  • 2
  • 9
  • 21
0

Try use Broadcast receiver: . Is necessary register broadcast. See: Problem with SMS Broadcast receiver

Community
  • 1
  • 1
  • I have a broadcast receiver that fires when a new SMS comes in but I need to see when the message gets put into the provider, this does not happen right away – tyczj Jan 21 '13 at 02:10
  • +1 tyczj, there is no existing Intent for outgoing SMS/MMS so we can't catch them with a BroadcastReceiver. We are forced to use an Observer which will be notified of changes (both for received/sent SMS/MMS) – Alex Aug 04 '16 at 09:01
0

If you are using brodcast receiver, sms is put in to db when brodcast ends. This means you need to fire bg thread and sleep it for litle time and then sms will show in db

Adam Fręśko
  • 1,064
  • 9
  • 14