发送超过160个字符的短信

时间:2017-01-18 12:48:01

标签: xamarin xamarin.android sms

发送超过150个字符的短信我没什么问题。

我找到了这段代码,但我不知道如何从android重写到xamarin android。

代码:

SmsManager sm = SmsManager.getDefault();
ArrayList<String> parts =sm.divideMessage(LONG_TEXT);
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(PendingIntent.getBroadcast(getContext(), 0, mSendIntent, 0));
deliveryIntents.add(PendingIntent.getBroadcast(getContext(), 0, mDeliveryIntent, 0));
sm.sendMultiPartTextMessage(mDestAddr,null, parts, sentIntents, deliveryIntents)

这是我的代码:

SmsManager smsManager = SmsManager.Default;
            IList<string> parts = smsManager.DivideMessage(responseModel.message);
            var sent = PendingIntent.GetBroadcast(Application.Context, 0, new Intent("SMS_SENT"), 0);
            SmsManager.Default.SendMultipartTextMessage(responseModel.receipent, null, parts, sent, null);

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以将字符串拆分为一定长度的块,然后使用SendMultipartTextMessage发送长文本。例如:

var content = "**HERE GOES SMS CONTENT**";
var destinationAdd = "**HERE GOES DESTINATION PHONE NUMBER**";

SmsManager sm = SmsManager.Default;
if (content.Length >= 150)
{
    List<string> parts = new List<string>();
    //split the string into chunks of 20 chars.
    var enumerable = Enumerable.Range(0, content.Length / 20).Select(i => content.Substring(i * 20, 20));
    parts = enumerable.ToList();
    sm.SendMultipartTextMessage(destinationAdd, null, parts, null, null);
}
else
{
    sm.SendTextMessage(destinationAdd, null, content, null, null);
}