Skip to content Skip to sidebar Skip to footer

How To Read Incoming Mms And Sms Messages On Android

I know this is a big topic, as seen here and here, so I just wanted to post how I solved both the issue of receiving incoming MMS and SMS messages and the issue of grabbing data fr

Solution 1:

Here is a code snippet to show how to parse incoming SMS data with a Broadcast Receiver:

publicoverridevoidOnReceive(Context context, Intent intent)
    {
        Log.Info(TAG, "Intent action received: " + intent.Action);

        // Retrieve message from the intent and analyze it.
        SmsMessage msg = Android.Provider.Telephony.Sms.Intents.GetMessagesFromIntent(intent)[0];
        string message = msg.DisplayMessageBody;
        (string, bool) result = MMSReceiver.CleanUpMessage(message);

        // If there were one or more rooster words.if (result.Item2)
        {
            string title = "Rooster Text Received From: " + msg.DisplayOriginatingAddress;
            DependencyService.Get<INotificationManager>().ScheduleNotification(title, result.Item1);
        }   
    }

And here is a code snippet to show how to parse incoming MMS data with a Broadcast Receiver:

public override voidOnReceive(Context context, Intent intent)
    {
        Log.Info(TAG, "Intent action received: " + intent.Action);

        // Get the MMS ID. Adapted from: https://stackoverflow.com/questions/10065249/how-to-get-mms-id-android-applicationContentResolver contentResolver = AndroidApp.Context.ContentResolver;
        Android.Net.Uri mmsInboxUri = Android.Net.Uri.Parse("content://mms");
        Android.Database.ICursor mmsInboxCursor = contentResolver.Query(mmsInboxUri, newstring[]
            {"_id","msg_box","ct_t","date"}, "msg_box=1 or msg_box=2", null, null);
        int id = -1;
        if (mmsInboxCursor != null)
        {
            try
            {
                if (mmsInboxCursor.MoveToFirst())
                {
                    id = Int32.Parse(mmsInboxCursor.GetString(0));
                    Log.Info(TAG, "Id is this: " + mmsInboxCursor.GetString(0));
                }
            }
            catch (System.Exception error)
            {
                Log.Error(TAG, "Error requesting the MMS ID: " + error.Message);
            }
        }// if (mmsInboxCursor != null)// Get text and picture from MMS message. Adapted from: https://stackoverflow.com/questions/3012287/how-to-read-mms-data-in-androidstring message = ""; // textAndroid.Graphics.Bitmap bitmap = null; // picturestring selectionPart = "mid=" + id;
        Android.Net.Uri mmsTextUri = Android.Net.Uri.Parse("content://mms/part");
        Android.Database.ICursor cursor = contentResolver.Query(mmsTextUri, null,
            selectionPart, null, null);
        if (cursor.MoveToFirst())
        {
            do
            {
                string partId = cursor.GetString(cursor.GetColumnIndex("_id"));
                stringtype = cursor.GetString(cursor.GetColumnIndex("ct"));
                // Get text.if ("text/plain".Equals(type))
                {
                    string data = cursor.GetString(cursor.GetColumnIndex("_data"));
                    
                    if (data != null)
                    {
                        message = GetMmsText(partId);
                        Log.Info(TAG, "Body is this: " + message);
                    }
                    else
                    {
                        message = cursor.GetString(cursor.GetColumnIndex("text"));
                        Log.Info(TAG, "Body is this: " + message);
                    }
                }
                //Get picture.if ("image/jpeg".Equals(type) || "image/bmp".Equals(type) ||
                        "image/gif".Equals(type) || "image/jpg".Equals(type) ||
                        "image/png".Equals(type))
                {
                    bitmap = GetMmsImage(partId);
                }
            } while (cursor.MoveToNext());
        }// if (cursor.MoveToFirst())
    }

Post a Comment for "How To Read Incoming Mms And Sms Messages On Android"