KVISH KVISH - 1 year ago 131
Android Question

Android MMS observer

I have the following code:

public class MmsObserver extends ContentObserver {
private Context context;

public MmsObserver(Handler handler) {
super(handler);
this.context = service.getBaseContext();
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);

Long largestDateCounted = Long.parseLong(UserPreferencesManager.getInstance().getValueFromPreferences(context, context.getString(R.string.preferences_current_counter), "0"));
String filter = "creator != ? and date > ?";
String[] args = new String[]{context.getPackageName(), Long.toString(largestDateCounted)};
Cursor c = context.getContentResolver().query(Constants.Mms, null, filter, args, null);

try {

} catch (Exception e) {
e.printStackTrace();
} finally {
c.close();
}
}
}


I'm trying to observe when user sends/receives an MMS message. However, my observer never gets called. Is there something I'm missing on this? I have read the below:

Android MMS Monitoring

Android MMS Broadcast receiver

EDIT

here is how i'm running the observer:

mmsContent = new MmsObserver(new Handler());
getContentResolver().registerContentObserver(Constants.Mms, true, mmsContent);

Answer Source

When registering a ContentObserver for MMS, the URI needs to be content://mms-sms/, at least on older Android versions. For some reason, content://mms/ won't work for a ContentObserver, other than possibly firing on changes to draft messages.

Do note that this will cause the Observer to fire for changes to the SMS table, as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download