Android MMS Broadcast receiver

mehdouch picture mehdouch · May 27, 2011 · Viewed 9.3k times · Source

I am working on a MMS broadcast receiver. It already starts when receiving a MMS but I dont know how to capture / parse the contents of the mms like it is done with sms in this example:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;

public class SMSBroadcastReceiver extends BroadcastReceiver {

        private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
        private static final String TAG = "SMSBroadcastReceiver";

        @Override
        public void onReceive(Context context, Intent intent) {
             Log.i(TAG, "Intent recieved: " + intent.getAction());

                if (intent.getAction() == SMS_RECEIVED) {
                    Bundle bundle = intent.getExtras();
                    if (bundle != null) {
                        Object[] pdus = (Object[])bundle.get("pdus");
                        final SmsMessage[] messages = new SmsMessage[pdus.length];
                        for (int i = 0; i < pdus.length; i++) {
                            messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        }
                        if (messages.length > -1) {
                            Log.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                        }
                    }
                }
           }
    }

thanks

Answer

mdelolmo picture mdelolmo · May 27, 2011

After reading a couple of related questions:

Detecting new MMS (Android 2.1)
Detecting MMS messages on Android

It seems this feautre is mostly supported, but not officially, so you wouldn't find much on the documentation. So, one of the links provided within those related questions points to something which looks like something you may be interested in.

Specially interesting this piece of code:

   public void startMMSMonitoring() {
      try {
         monitorStatus = false;
         if (!monitorStatus) {
            contentResolver.registerContentObserver(Uri.parse("content://mms-sms"), true, mmsObserver);

            Uri uriMMSURI = Uri.parse("content://mms");
            Cursor mmsCur = mainActivity.getContentResolver().query(uriMMSURI, null, "msg_box = 4", null, "_id");
            if (mmsCur != null && mmsCur.getCount() > 0) {
               mmsCount = mmsCur.getCount();
               Log("", "MMSMonitor :: Init MMSCount ==" + mmsCount);
            }
         }
      } catch (Exception e) {
         Log("", "MMSMonitor :: startMMSMonitoring Exception== "+ e.getMessage());
      }
   }

What if you have a look, test and give us some feedback?

regards.