Xamarin.Android: 在 SMS Delivered Broadcast Receiver 中检测 SMS
Xamarin.Android: Detect SMS in SMS Delivered Broadcast Receiver
在我的 Xamarin.Android
应用程序中,我使用 SmsManager
class 发送短信。我还使用 PendingIntent
和 BroadcastReceiver
.
获取交付状态
一切正常,除了调用 SMSDeliveredReceiver.OnReceive 时我无法判断发送了哪条短信。假设我发送了两条消息,我只知道其中一条已发送。我需要知道发送了哪条 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;
}
}
}
在你的SendTextMessage
方法中使用Intent
传递数据,当你的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;
}
}
}
在我的 Xamarin.Android
应用程序中,我使用 SmsManager
class 发送短信。我还使用 PendingIntent
和 BroadcastReceiver
.
获取交付状态
一切正常,除了调用 SMSDeliveredReceiver.OnReceive 时我无法判断发送了哪条短信。假设我发送了两条消息,我只知道其中一条已发送。我需要知道发送了哪条 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;
}
}
}
在你的SendTextMessage
方法中使用Intent
传递数据,当你的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;
}
}
}