我使用 Xamarin Studio 和 SMS.Telephony.SmsManager Android 库成功通过 Android 发送多部分(串联)消息。
要发送消息,我正在执行以下操作:
var longMessage = "This is a cØncatenated message sent to you through an android. This should appear as a single message. Hopefully it's as easy as that. It even has a function to break the message up. It probably took me longer to install Xamarin then it did to write the code and send the actual message";
var smsMgr = Android.Telephony.SmsManager.Default;
System.Collections.Generic.IList<string> parts = smsMgr.DivideMessage(longMessage);
IList<PendingIntent> pendingIntents = new List<PendingIntent>(parts.Count);
for (int i = 0; i < parts.Count; i++)
{
var intent = new Intent(DeliveryIntentAction);
intent.PutExtra("MessagePartText", parts[i]);
intent.PutExtra("MessagePartId", i.ToString());
PendingIntent pi = PendingIntent.GetBroadcast(this, 0, intent, 0);
pendingIntents.Add(pi);
}
smsMgr.SendMultipartTextMessage("xxx",null, parts, pendingIntents, null);
然后我有一个用于待处理意图的接收器,如下所示:
[BroadcastReceiver(Enabled = true)] //(Exported = true, Permission = "//receiver/@android:android.permission.SEND_SMS")]
[IntentFilter(new[] { DeliveryIntentAction }, Priority = int.MaxValue)]
public class SMSSentReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.GetStringExtra ("MessagePartId") != null)
lbl.Text += " Sent Response " + intent.GetStringExtra ("MessagePartId") + System.Environment.NewLine;
...
但是,每次调用 OnRecieve 时,intent.GetStringExtra("MessagePartId") 的值为“0”,并且 MessagePartText 是第一个部分,而不是属于发送的消息部分的部分编号/文本。
有人能明白为什么会出现这种情况吗?
谢谢你
使用 PendingIntent pi = PendingIntent.GetBroadcast(this, 0, 意图, 0); 尝试覆盖现有的pendingIntent。
要创建一个新意图并让它等待,我必须更改 GetBroadcast 的 requestCode 参数。
PendingIntent pi = PendingIntent.GetBroadcast(this, i, 意图, 0);
谢谢