Share via

How do you read an SMS and MMS message using .NET MAUI (on Android only)?

Gene Magpayo 20 Reputation points
2024-06-19T23:12:35.5966667+00:00

I'd like to read an SMS and/or MMS message on an Android device only, but from within my .NET MAUI app.

Specifically, I want to save any received (MMS) picture that has been texted to me plus any associated (SMS) text. Is this possible with .NET MAUI and is there a code sample for review on this? Thank you very much!

Developer technologies | .NET | .NET Multi-platform App UI
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

Locked Question. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2024-06-20T08:30:19.4566667+00:00

Hello,

I'd like to read an SMS and/or MMS message on an Android device only,

Firstly, before you read the SMS/MMS, you need to add the permission in the <manifest> tag of Platforms/Android/Resources/AndroidManifest.xml and request permission.

Read the SMS and request permission, you can refer to this link: Access and read all SMS on Android phone with .NET MAUI

Read the MMS, you can get ID by MMS Attachment Table, then you can get Image stream by id in MMS inbox. Because this MMS file type will be text, image or others, we need to judge the file type.

In the end, we can get the stream by Stream resStream = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.OpenInputStream(PartURI1);, if you have many images, you can save this image stream in the List<Stream>.

            var res = await CheckAndRequestSMSPermission();
            if (res.Equals(PermissionStatus.Granted))
            {
#if ANDROID
                Android.Net.Uri CONTENT_URI = Android.Net.Uri.Parse("content://mms/inbox"); 

//Check your MMS inbox
                Android.Net.Uri CONTENT_URI_PART = Android.Net.Uri.Parse("content://mms/part"); 

//Check your MMS inbox
                Android.Database.ICursor cursor = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.Query(CONTENT_URI, null, null, null, null);

                List<int> mmsIDs = new List<int>();
                List<string> mmsSubjects = new List<string>();

                if (cursor.MoveToFirst())
                {
                    do
                    {
                        int id = cursor.GetInt(cursor.GetColumnIndex("_id"));//MMS Id
                        mmsIDs.Add(id);
                        string subject = cursor.GetString(cursor.GetColumnIndex("sub")); //MMS theme
                        mmsSubjects.Add(subject);

                    } while (cursor.MoveToNext());
                }

                if (mmsIDs.Count > 0)
                {
                    foreach (int id in mmsIDs)
                    {

                        string selection = "mid=" + id; //this key is the mms id。
                        Android.Database.ICursor cPart = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.Query(CONTENT_URI_PART, null, selection, null, null);
                        Android.Net.Uri partURI = Android.Net.Uri.Parse("content://mms/part/" + "_id");

                        List<Stream> allMMSFileStream = new List<Stream>();

                        String bodyStr = "";
                        String[] coloumns = null;
                        String[] values = null;
                        while (cPart.MoveToNext())
                        {
                            coloumns = cPart.GetColumnNames();
                            if (values == null)
                                values = new string[coloumns.Length];
                            for (int i = 0; i < cPart.ColumnCount; i++)
                            {
                                values[i] = cPart.GetString(i);
                            }
                            if (values[3].Equals("image/jpeg") || values[3].Equals("image/bmp") || values[3].Equals("image/gif") || values[3].Equals("image/jpg") || values[3].Equals("image/png"))
                            {  //judge the file type, if the file type is image.
                             
                                Android.Net.Uri PartURI1 = Android.Net.Uri.Parse("content://mms/part/" + values[0]);
                                Stream resStream = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.OpenInputStream(PartURI1);
                                if(resStream != null)
                                {
                                    //save the Image Stream to List
                                    allMMSFileStream.Add(resStream);
                                }
                                //I make a ImageView to make a test.
                                myImage.Source = ImageSource.FromStream(()=> resStream);
                            }



                        }
                    }
                }
#endif

Best Regards,

Leon Lu


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more