tags:

views:

944

answers:

2

How can I register for SMS database changes?

I tried:

mCursor = mActivity.getContentResolver().query(Sms.CONTENT_URI, new String[] {
           Sms.ADDRESS
   }, null, null, null);
mCursor.registerDataSetObserver(mydataSetObserver);

where mydataSetObserver is implemented like this:

 private class MyDataSetObserver extends DataSetObserver {
       public void onChanged() {
           System.out.println ("1");
       }
       public void onInvalidated() {
            System.out.println ("2");
       }
}

But when I tried sending a SMS message in the emulator, MyDataSetObserver never get called.

Can you please tell me why?

Thank you.

+1  A: 

DataSetObservers only observe DataSetObservables they are registered with. Your MyDataSetObserver is registered with your mCursor and will be notified whenever mCursor changes (e.g. after requery) but not when the content is written by another process (like the Messaging application).

Unfortunately there is currently no good way to listen for the event of sent text messages, the best alternative seems to be polling content://sms/sent, potentially using a ContentObserver.

This question is related.

Josef
How can I get notified when a SMS is read?And what do you mean by 'polling content://sms/sent'?
n179911
By polling I mean the frequent reading and checking of the sms content provider (e.g. with a timer)
Josef
+2  A: 

It sounds like all you are trying to do is have the ability to make changes to the SMS database on the device.

The way I have done it in the past is by using tags in the AndroidManifest.xml. The application I made needed to use the READ_SMS permission as well as the READ_CONTACTS permission, however gaining permission for writing to the database would be done in the same way.

I defined these desired permissions in the AndroidManifest.xml file with the following tag:

Included in the list of permissions you can use is WRITE_SMS, which should give you the desired capability.

Please note: because I am a new user, StackOverflow would only let me post one hyperlink for this post, I tried including alot more information however was unable to do so. Please go to the android developer website and search for the AndroidManifest.xml file and see more info if need be.

segfault