Reputation: 341
I am trying to delete received SMS in ContentObserver (on "content://sms/") onChange() method using contentResolver.delete, but after deleting ContentObserver is invoked one more time.
Could you explaing my, whe ContentObserver invokes itself despite deliverSelfNotifications() returns false?
I foud soultion to uregister ContentObserver before deleting the sms and register again after, but is inelegant.
My code:
private class SmsMmsContentObserver extends ContentObserver {
public SmsMmsContentObserver(Handler handler)
{
super(handler);
}
@Override public boolean deliverSelfNotifications() {
return false;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Uri uriSMSURI = Uri.parse("content://sms/");
Cursor cur = getContentResolver().query(uriSMSURI, null, null,
null, null);
if (cur.moveToNext())
{
int threadIdIn = cur.getInt(cur.getColumnIndex("thread_id"));
getContentResolver().delete(Uri.parse("content://sms/conversations/" + threadIdIn), null, null);
}
}
}
Methods to register observer in on of services.
private void registerSmsMmsObserver()
{
if (observer == null)
{
observer = new SmsMmsContentObserver(new Handler());
contentResolver = getContentResolver();
contentResolver.registerContentObserver(uriSmsMms, true, observer);
}
}
private void unregisterSmsMmsObserver()
{
if (contentResolver != null)
contentResolver.unregisterContentObserver(observer);
observer = null;
}
Upvotes: 3
Views: 2040
Reputation: 39406
If you want to delete the SMS when it arrives in the inbox, use a receiver for the orderedbroadcast that matches the SMS receive intent, get a higher priority than the regular inbox, and cancel the broadcast.
Also, the delete is sure to invoke an onChange on any contentObserver obsering this URI.
deliverSelfNotification does not do what you seem to think:
Returns true if this observer is interested in notifications for changes made through the cursor the observer is registered with.
Upvotes: 1