gpt4 book ai didi

c# - Xamarin C# 读取消息 |覆盖错误

转载 作者:行者123 更新时间:2023-11-29 20:00:25 26 4
gpt4 key购买 nike

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

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

代码:

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>

最佳答案

那是因为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));
}
}
}

关于c# - Xamarin C# 读取消息 |覆盖错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36355785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com