gpt4 book ai didi

android - Xamarin.Android:在 SMS Delivered Broadcast Receiver 中检测 SMS

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

在我的 Xamarin.Android 应用程序中,我使用 SmsManager 类发送短信。我还使用 PendingIntentBroadcastReceiver 获取传递状态。
一切正常,除了在调用 SMSDeliveredReceiver.OnReceive 时我无法判断发送了哪个 SMS。假设我发送了两条消息,我只知道其中一条已发送。我需要知道发送了哪条 SMS 以进行进一步处理。请告诉我如何在 SMS 和 Delivery 之间建立桥梁。

到目前为止,这是我的代码:

private PendingIntent piSent, piDelivered;
private BroadcastReceiver _smsSentBroadcastReceiver, _smsDeliveredBroadcastReceiver;

void SetUp()
{
piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0);
piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);

_smsSentBroadcastReceiver = new SMSSentReceiver();
_smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver();

RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT"));
RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED"));

}

void Send(string number, string message)
{
SmsManager.Default.SendTextMessage(q.Number, null, q.Message, piSent, piDelivered);
}

[BroadcastReceiver(Exported = true)]
public class SMSDeliveredReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(Application.Context, "SMSDeliveredReceiver.OnReceive", ToastLength.Short).Show();
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.MakeText(Application.Context, "SMS Delivered", ToastLength.Short).Show();
break;
case (int)Result.Canceled:
Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
break;
default:
Toast.MakeText(Application.Context, ResultCode.ToString(), ToastLength.Short).Show();

break;
}
}

}

最佳答案

使用Intent 在您的SendTextMessage 方法中传递数据,当您的SMSDeliveredReceiver.OnReceive 被调用时,您可以知道哪个SMS 被传递intent.GetStringExtra("电话")

这是完整的代码,它运行良好:

[Activity(Label = "SMSDemo", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private static string ACTION_SENT_SMS = "ACTION_SENT_SMS";

private PendingIntent piSentd;
SMSDeliveredReceiver mReceiver;

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.button1);

button.Click += (sender, e) =>
{
sendGroupSMS();
};

mReceiver = new SMSDeliveredReceiver();
RegisterReceiver(mReceiver, new IntentFilter(ACTION_SENT_SMS));
}

protected override void OnDestroy()
{
base.OnDestroy();
UnregisterReceiver(mReceiver);
}

void sendGroupSMS()
{
List<string> phones = new List<string>();
phones.Add("1*****2");
phones.Add("1*****8");
phones.Add("1*****9");

SmsManager smsMgr = SmsManager.Default;
foreach(string phone in phones)
{
Log.Debug("TAG", "Start sending sms to the phone " + phone);
Intent intent = new Intent(ACTION_SENT_SMS);
intent.PutExtra("phone", phone);

piSentd = PendingIntent.GetBroadcast(this, phone.GetHashCode(), intent, PendingIntentFlags.UpdateCurrent);
SmsManager.Default.SendTextMessage(phone, null, "Test by York", piSentd, null);
}

}
}

[BroadcastReceiver(Exported = true)]
public class SMSDeliveredReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string phone = intent.GetStringExtra("phone");
switch ((int)ResultCode)
{
case (int)Result.Ok:
Toast.MakeText(Application.Context, "SMS Delivered" + phone + " success.", ToastLength.Short).Show();
break;
case (int)Result.Canceled:
Toast.MakeText(Application.Context, "SMS not delivered", ToastLength.Short).Show();
break;
default:
Toast.MakeText(Application.Context, ResultCode.ToString(), ToastLength.Short).Show();
break;
}
}

}

关于android - Xamarin.Android:在 SMS Delivered Broadcast Receiver 中检测 SMS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44436020/

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