Lorenzo Howar
Lorenzo Howar

Reputation: 11

Xamarin C# Read Message | Override error

I started to write a code to send and receive messages from my Smartphone (Android) when i tried to send SMS I not had any problem, but when I tried receive SMS I get an error that says

MainActivity.OnReceive (Context, Intent): no suitable method found to override

The code:

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>

Upvotes: 1

Views: 1742

Answers (1)

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

That is because OnReceive is not a method of Activity. And this means you can't override it. You have to move your code into an class that inherits from 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));
        }
    }
}

Upvotes: 2

Related Questions