2

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);
Community
  • 1
  • 1
KVISH
  • 12,923
  • 17
  • 86
  • 162
  • How exactly are you registering the Observer? Are you certain your `Service` (presumably) is running when the changes occur? – Mike M. Aug 22 '16 at 04:54
  • I updated my question. Yes, service is running and I have an SMS observer which is also always running (and working). – KVISH Aug 22 '16 at 04:57
  • Oh, sorry, I was asking mainly for the specific `Uri` you're using. It needs to be `content://mms-sms/`, at least on older versions. I've not dug through the source to find out why, but for some reason, `content://mms/` won't work for a `ContentObserver`. Do note that that's going to fire for SMS, too. – Mike M. Aug 22 '16 at 05:03
  • 1
    That was correct! I will accept your answer if you put it below. – KVISH Aug 22 '16 at 05:06
  • whats strange is that on some phones `content://mms-sms/` doesn't work. At least when querying for new messages. On Galaxy S6 it didn't work. Will have to test this on that device. – KVISH Aug 22 '16 at 05:21
  • The query should work with `content://mms/`. You just need to register the Observer with `content://mms-sms/`. – Mike M. Aug 22 '16 at 05:25

1 Answers1

1

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.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • Hi Mike, you have answered a few of my questions on SO and wanted to ask you something. I have a project here: https://github.com/kalvish21/AndroidMessenger, my goal is to create a desktop client for an Android app that can do SMS and MMS from desktop. I want to open source it and make it free. Are you interested in helping out? The mac client is almost done. Basic UI improvements will happen on Android app. – KVISH Aug 28 '16 at 20:07
  • Next step is desktop client for Windows and Linux. – KVISH Aug 28 '16 at 20:08
  • Please reply either way, would love to hear from you. – KVISH Aug 28 '16 at 20:18
  • Hi, there! I'd love to help, but I don't have access to an actual development machine, at the moment. I've been doing minor stuff directly on my mobile devices for a while, but I can't really do anything for desktop. I'll keep a bookmark to your stuff, though, and give you a shout if I ever get setup again. Good luck with your project! Sounds like fun. Cheers! – Mike M. Aug 28 '16 at 20:30