使用MonoDroid接收短信

时间:2013-01-07 16:35:48

标签: c# android android-intent xamarin.android xamarin

编辑:现在已经解决了 - 在此处发布解决方案以防将来有人需要它,或者任何人都可以建议更好的方法。我从清单中删除了intent的东西,然后在我的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 Mananger发送工作。

我现在正试图在Mono for Android中接收短信,并且是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函数在intent中根据我读过的在线教程我需要({{3 }})。

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



        }
    }
}

我很欣赏我在Android和MonoDroid上有点过头但是也许有人可以指出我正确的方向!

1 个答案:

答案 0 :(得分:9)

我这里有一些工作代码(我不记得来源,但我认为是与Xamarin有关的人写了这个样本),当收到短信时会显示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>