我试图用Xamarin在Android上处理新的短信。 我使用真实设备,而不是虚拟设备。 我的处理程序是:
public class SmsBroadcastReceiver : BroadcastReceiver
{
...
}
我通过以下方式在主要活动中以编程方式注册接收器:
RegisterReceiver(new SmsBroadcastReceiver(), new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
在我的代码中,我为意图10k +添加优先级(没有尝试过这个)。我也试过通过配置注册。 接收器不会发射。 但是,如果我注册HEADSET_PLUG事件,它将毫无问题地开火。 权限是:
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
答案 0 :(得分:0)
你能试试吗:
[BroadcastReceiver]
[Android.App.IntentFilter(new []{ SMS_RECEIVER })]
public class SmsBroadcastReceiver : BroadcastReceiver
{
private const string SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";
...
}
顺便说一下,我还没有测试过,但我相信它会起作用。请让我知道它是怎么回事(:
答案 1 :(得分:0)
试试这个应该可以正常工作:
[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;
SmsMessage[] messages=Telephony.Sms.Intents.GetMessagesFromIntent (intent);
var sb = new StringBuilder();
for (var i = 0; i < messages.Length; i++)
{
sb.Append(string.Format("SMS From: {0}{1}Body: {2}{1}", messages[i].OriginatingAddress,
Environment.NewLine,messages[i].MessageBody));
}
}
}