编辑:现在已解决 - 在此处发布解决方案以防将来有人需要它,或者任何人都可以提出更好的方法。我从清单中删除了意图内容,并在我的 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 有点不知所措,但也许有人可以为我指明正确的方向!