Android MMS Broadcast receiver

▼魔方 西西 提交于 2019-12-30 07:09:28

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/6151749/android-mms-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!