Xamarin C#阅读消息|覆盖错误

时间:2016-04-01 11:59:03

标签: c# android xamarin sms xamarin.android

当我尝试发送短信时,我开始编写代码来发送和接收来自我的智能手机(Android)的消息我没有任何问题,但当我尝试接收短信时,我收到错误消息

  

MainActivity.OnReceive(Context,Intent):找不到合适的方法   超控

代码:

using System;
using System.Text;
using Android.App;
using Android.Content;
using Android.Widget;
using Android.OS;
using Android.Telephony;
using Android.Util;
using Environment = System.Environment;
using Android.Provider;

namespace Mensajes
{


[Activity(Label = "Mensajes", MainLauncher = true, Icon = "@drawable/icon")]
[BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]

public class MainActivity : Activity
{  
    private const string Tag = "SMSBroadcastReceiver";
    private const string IntentAction =   "android.provider.Telephony.SMS_RECEIVED";
    AutoCompleteTextView edittext; 
    TextView textView;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        Button button2 = FindViewById<Button>(Resource.Id.button1);
        edittext = FindViewById<AutoCompleteTextView>(Resource.Id.MyTextEdit);
        textView = FindViewById<TextView>(Resource.Id.MyTextView);
        button2.Click += delegate { EnviaMensaje(); };
    }

    protected 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));
        }
    }

    private void EnviaMensaje()
    {
        SmsManager.Default.SendTextMessage("123456789", null,
        edittext.Text, null, null);
    }
  }
} 

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
   <manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="Mensajes.Mensajes" android:versionCode="1" android:versionName="1.0">
      <uses-sdk android:minSdkVersion="16" />
      <uses-permission android:name="android.permission.SEND_SMS" />
      <uses-permission android:name="android.permission.READ_SMS" />
      <uses-permission android:name="android.permission.RECEIVE_SMS" />
      <application android:label="Mensajes"></application>
 </manifest>

1 个答案:

答案 0 :(得分:2)

这是因为OnReceive不是Activity的方法。这意味着你无法覆盖它。您必须将代码移动到继承自BroadcastReceiver的类中。

[BroadcastReceiver(Enabled = true, Label = "SMS Receiver")]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED" })]
public class IncomingSms : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Log.Info(Tag, "Intent received: " + intent.Action);
        if (intent.Action != "android.provider.Telephony.SMS_RECEIVED") 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));
        }
    }
}