5

编辑:现在已解决 - 在此处发布解决方案以防将来有人需要它,或者任何人都可以提出更好的方法。我从清单中删除了意图内容,并在我的 SmsReceiver 类中设置了 BroadcastReceiver。这现在有效。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;
using Android.Telephony;

namespace dummyAndroid
{
    [BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
    [IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED" })] 
    public class SmsReceiver : Android.Content.BroadcastReceiver 
    {
        public static readonly string INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED"; 

        public override void OnReceive(Context context, Intent intent)
        {
            if (intent.Action == INTENT_ACTION)
            {
                StringBuilder buffer = new StringBuilder();
                Bundle bundle = intent.Extras;

                if (bundle != null)
                {
                    Java.Lang.Object[] pdus = (Java.Lang.Object[])bundle.Get("pdus");

                    SmsMessage[] msgs;
                    msgs = new SmsMessage[pdus.Length];

                    for (int i = 0; i < msgs.Length; i++)
                    {
                        msgs[i] = SmsMessage.CreateFromPdu((byte[])pdus[i]);

                        Log.Info("SmsReceiver", "SMS Received from: " + msgs[i].OriginatingAddress);
                        Log.Info("SmsReceiver", "SMS Data: " + msgs[i].MessageBody.ToString());
                    }

                    Log.Info("SmsReceiver", "SMS Received");
                }
            } 
        }
    }
}

我正在编写一个发送/接收 SMS 消息并通过 SMS Mananger 发送工作的应用程序。

我现在正在尝试在 Mono for Android 中接收 SMS,并且是 Android 开发的新手,所以可能做错了什么!

我已将以下内容添加到我的清单中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto" package="com.me.dummyAndroid" android:versionCode="1" android:versionName="1">
  <uses-sdk android:targetSdkVersion="8" />
  <application 
        android:label="meAndroidSMS"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
    <receiver android:name=".SmsReceiver">
      <intent-filter>
        <action android:name=
                    "android.provider.Telephony.SMS_RECEIVED" />
      </intent-filter>
    </receiver>
  </application>
  <uses-permission android:name="android.permission.SEND_SMS" />
  <uses-permission android:name="android.permission.RECEIVE_SMS" />
  <uses-permission android:name="android.permission.READ_SMS" />
</manifest>

然后,我创建了一个名为 SmsReceiver.cs 的新类,我已将函数 onReceive 添加到其中,但意图中似乎没有 getExtras 函数,根据我阅读的在线教程,我需要(http://www. techques.com/question/1-3542320/iPhone-Android-SMS-intercept-and-redirection-to-an-application .)。

namespace dummyAndroid
{
    class SmsReceiver
    {
        public void onReceive(Context context, Intent intent)
        {
            Bundle bundle = intent.getExtras();



        }
    }
}

我很感激我肯定对 Android 和 MonoDroid 有点不知所措,但也许有人可以为我指明正确的方向!

4

1 回答 1

9

我这里有一些工作代码(我不记得源代码,但我认为是与 Xamarin 相关的人编写了此示例),当收到 SMS 时显示 Toast。

using System.Text;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using Android.Telephony;
using Environment = System.Environment;

namespace MonoDroid.SMSFun
{
    [BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
    [IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })] 
    public class SMSBroadcastReceiver : BroadcastReceiver
    {
        private const string Tag = "SMSBroadcastReceiver";
        private const string IntentAction = "android.provider.Telephony.SMS_RECEIVED"; 

        public override void OnReceive(Context context, Intent intent)
        {
            Log.Info(Tag, "Intent received: " + intent.Action);

            if (intent.Action != IntentAction) return;

            var bundle = intent.Extras;

            if (bundle == null) return;

            var pdus = bundle.Get("pdus");
            var castedPdus = JNIEnv.GetArray<Java.Lang.Object>(pdus.Handle);

            var msgs = new SmsMessage[castedPdus.Length];

            var sb = new StringBuilder();

            for (var i = 0; i < msgs.Length; i++)
            {
                var bytes = new byte[JNIEnv.GetArrayLength(castedPdus[i].Handle)];
                JNIEnv.CopyArray(castedPdus[i].Handle, bytes);

                msgs[i] = SmsMessage.CreateFromPdu(bytes);

                sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", msgs[i].OriginatingAddress,
                                        Environment.NewLine, msgs[i].MessageBody));
            }

            Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show();
        }
    }
}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="internalOnly" package="monodroid.smsfun" android:versionCode="1" android:versionName="1.0">
  <uses-sdk android:targetSdkVersion="8" />
  <application android:label="SMSFun">
  </application>
  <uses-permission android:name="android.permission.RECEIVE_SMS" />
</manifest>
于 2013-01-08T10:08:05.770 回答