如何在Android上读取收到的MMS和SMS消息

huangapple go评论101阅读模式
英文:

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 from those MMS and SMS messages on Android 9.0 version 28+ using Xamarin.Forms. This code can easily be translated to Java. Here is the completed Android app so you can try it yourself. It also shows how to do some Azure machine learning if you're interested in that.

For Broadcast Receivers:
Classes, registering class instances , permissions needed.

Note that the broadcast receivers were added dynamically, they can be added statically using Xamarin's intent-filter decorator , or (if you're not using Xamarin) the AndroidManifest.xml file.

英文:

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 from those MMS and SMS messages on Android 9.0 version 28+ using Xamarin.Forms. This code can easily be translated to Java. Here is the completed Android app so you can try it yourself. It also shows how to do some Azure machine learning if you're interested in that.

For Broadcast Receivers:
Classes, registering class instances , permissions needed.

Note that the broadcast receivers were added dynamically, they can be added statically using Xamarin's intent-filter decorator , or (if you're not using Xamarin) the AndroidManifest.xml file.

答案1

得分: 0

以下是一个代码片段,展示了如何使用广播接收器解析传入的短信数据:

public override void OnReceive(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);
    }   
}

以下是一个代码片段,展示了如何使用广播接收器解析传入的多媒体短信(MMS)数据:

public override void OnReceive(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-application
    ContentResolver contentResolver = AndroidApp.Context.ContentResolver;
    Android.Net.Uri mmsInboxUri = Android.Net.Uri.Parse("content://mms");
    Android.Database.ICursor mmsInboxCursor = contentResolver.Query(mmsInboxUri, new string[]
        {"_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-android
    string message = ""; // text
    Android.Graphics.Bitmap bitmap = null; // picture
    string 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"));
            string type = 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())
}

希望这有助于您理解如何使用广播接收器解析传入的短信和MMS数据。

英文:

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

public override void OnReceive(Context context, Intent intent)
{
Log.Info(TAG, &quot;Intent action received: &quot; + 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 = &quot;Rooster Text Received From: &quot; + msg.DisplayOriginatingAddress;
DependencyService.Get&lt;INotificationManager&gt;().ScheduleNotification(title, result.Item1);
}   
}

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

public override void OnReceive(Context context, Intent intent)
{
Log.Info(TAG, &quot;Intent action received: &quot; + intent.Action);
// Get the MMS ID. Adapted from: https://stackoverflow.com/questions/10065249/how-to-get-mms-id-android-application
ContentResolver contentResolver = AndroidApp.Context.ContentResolver;
Android.Net.Uri mmsInboxUri = Android.Net.Uri.Parse(&quot;content://mms&quot;);
Android.Database.ICursor mmsInboxCursor = contentResolver.Query(mmsInboxUri, new string[]
{&quot;_id&quot;,&quot;msg_box&quot;,&quot;ct_t&quot;,&quot;date&quot;}, &quot;msg_box=1 or msg_box=2&quot;, null, null);
int id = -1;
if (mmsInboxCursor != null)
{
try
{
if (mmsInboxCursor.MoveToFirst())
{
id = Int32.Parse(mmsInboxCursor.GetString(0));
Log.Info(TAG, &quot;Id is this: &quot; + mmsInboxCursor.GetString(0));
}
}
catch (System.Exception error)
{
Log.Error(TAG, &quot;Error requesting the MMS ID: &quot; + 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-android
string message = &quot;&quot;; // text
Android.Graphics.Bitmap bitmap = null; // picture
string selectionPart = &quot;mid=&quot; + id;
Android.Net.Uri mmsTextUri = Android.Net.Uri.Parse(&quot;content://mms/part&quot;);
Android.Database.ICursor cursor = contentResolver.Query(mmsTextUri, null,
selectionPart, null, null);
if (cursor.MoveToFirst())
{
do
{
string partId = cursor.GetString(cursor.GetColumnIndex(&quot;_id&quot;));
string type = cursor.GetString(cursor.GetColumnIndex(&quot;ct&quot;));
// Get text.
if (&quot;text/plain&quot;.Equals(type))
{
string data = cursor.GetString(cursor.GetColumnIndex(&quot;_data&quot;));
if (data != null)
{
message = GetMmsText(partId);
Log.Info(TAG, &quot;Body is this: &quot; + message);
}
else
{
message = cursor.GetString(cursor.GetColumnIndex(&quot;text&quot;));
Log.Info(TAG, &quot;Body is this: &quot; + message);
}
}
//Get picture.
if (&quot;image/jpeg&quot;.Equals(type) || &quot;image/bmp&quot;.Equals(type) ||
&quot;image/gif&quot;.Equals(type) || &quot;image/jpg&quot;.Equals(type) ||
&quot;image/png&quot;.Equals(type))
{
bitmap = GetMmsImage(partId);
}
} while (cursor.MoveToNext());
}// if (cursor.MoveToFirst())
}

huangapple
  • 本文由 发表于 2020年7月31日 22:48:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63194135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定