我使用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("17057178131",null, parts, pendingIntents, null);
然后我有一个接收器用于待处理的Intents,如下所示:
[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是第一部分,而不是属于发送的消息部分的部件号/文本。
有人能看出为什么会出现这种情况吗?
谢谢
答案 0 :(得分:3)
使用 PendingIntent pi = PendingIntent.GetBroadcast(this,0,intent,0); 尝试覆盖现有的pendingIntent。
要创建新意图并等待,我必须更改GetBroadcast的requestCode参数。
PendingIntent pi = PendingIntent.GetBroadcast(this,i,intent,0);
由于