~phroa/phone-app

b8966d1d16b4972b08a65fae5fb2c00af0e82e90 — Jack Stratton 3 years ago 15eef81
Receive SMS and MMS in separate classes
4 files changed, 28 insertions(+), 25 deletions(-)

M phms/MainActivity.cs
M phms/Properties/AndroidManifest.xml
M phms/SmsReceiver.cs
M phms/phms.csproj
M phms/MainActivity.cs => phms/MainActivity.cs +0 -2
@@ 2,8 2,6 @@
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;

namespace phms
{

M phms/Properties/AndroidManifest.xml => phms/Properties/AndroidManifest.xml +1 -0
@@ 6,6 6,7 @@
  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
  <uses-permission android:name="android.permission.RECEIVE_SMS" />
  <uses-permission android:name="android.permission.RECEIVE_MMS" />
  <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
  <application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme">
  </application>
</manifest>

M phms/SmsReceiver.cs => phms/SmsReceiver.cs +26 -23
@@ 1,42 1,45 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

using System.Collections.Generic;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Provider;
using Android.Util;

namespace phms
{
    [BroadcastReceiver(Enabled = true, Exported = true)]
    [IntentFilter(new[] {Telephony.Sms.Intents.SmsReceivedAction, Telephony.Sms.Intents.WapPushReceivedAction})]
    [IntentFilter(new[] {Telephony.Sms.Intents.SmsReceivedAction})]
    // ReSharper disable once UnusedType.Global
    public class SmsReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        public override void OnReceive(Context? context, Intent? intent)
        {
            switch (intent.Action)
            Log.Info("xxxxx", $"SMS Received {intent}");
            var messages = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
            foreach (var m in messages)
            {
                case Telephony.Sms.Intents.SmsReceivedAction:
                    Log.Info("xxxxx", $"SMS Received {intent}");
                    var messages = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
                    foreach (var m in messages)
                    {
                        Log.Info("xxxxx",
                            $"sms {m.OriginatingAddress} / {m.DisplayOriginatingAddress} / {m.MessageBody} / {m.DisplayMessageBody}");
                    }
                    break;
                case Telephony.Sms.Intents.WapPushReceivedAction:
                    Log.Info("xxxxx", $"WAP Push Received {intent}");
                    if (intent.Extras != null)
                    {
                        foreach (var key in intent.Extras.KeySet() ?? new List<string>())
                        {
                            Log.Info("xxxxx", $"mms bundle contains key {key}");
                        }
                    }
                    break;
                Log.Info("xxxxx",
                    $"sms {m.OriginatingAddress} / {m.DisplayOriginatingAddress} / {m.MessageBody} / {m.DisplayMessageBody}");
            }
        }
    }
    
    [BroadcastReceiver(Enabled = true, Exported = true, Permission = Manifest.Permission.BroadcastWapPush)]
    [IntentFilter(new[] {Telephony.Sms.Intents.WapPushReceivedAction, Telephony.Sms.Intents.WapPushDeliverAction}, DataMimeType = "application/vnd.wap.mms-message")]
    // ReSharper disable once UnusedType.Global
    public class WapReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context? context, Intent? intent)
        {
            Log.Info("xxxxx", $"received intent {intent} {intent?.Action} {intent?.Type}");
            if (intent?.Extras != null)
            {
                foreach (var key in intent.Extras.KeySet() ?? new List<string>())
                {
                    Log.Info("xxxxx", $"mms bundle contains key {key}");
                }
            }
        }
    }

M phms/phms.csproj => phms/phms.csproj +1 -0
@@ 22,6 22,7 @@
        <MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
        <MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
        <AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
        <Nullable>enable</Nullable>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <DebugSymbols>True</DebugSymbols>