在我的Xamarin.Android
应用中,我使用SmsManager
课程发送短信。我还使用PendingIntent
和BroadcastReceiver
来获取投放状态
一切正常,但我无法分辨调用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;
}
}
}
答案 0 :(得分:1)
使用Intent
以SendTextMessage
方式传递数据,当您SMSDeliveredReceiver.OnReceive
被调用时,您可以告诉我intent.GetStringExtra("phone")
发送了哪些短信。
这是完整的代码,它可以正常工作:
[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;
}
}
}