使用 Xamarin.Android 发送超过 160 个字符的短信

Send SMS with more than 160 characters using Xamarin.Android

我尝试了

的已接受答案中提到的代码

这没有按预期工作,在显示警报消息中点击 'Ok' 后没有发送短信。我找不到任何错误,不确定后台发生了什么。我没有足够的声誉来评论

void TestButton_Click (object sender, System.EventArgs e)
        {
            string message = editTextTx.Text;            

            if (((decimal)message.Length/ 160) == message.Length / 160)
                text_i = message.Length / 160;
            else
                text_i = (message.Length / 160) + 1;

            Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
            Android.App.AlertDialog alert = dialog.Create();
            alert.SetTitle("Warning");
            alert.SetMessage("It will need " + text_i.ToString() + " text message(s)");
            alert.SetButton("OK", (c, ev) =>
            {

                var destinationAdd = "**MY NUMBER**";

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


            });
            alert.Show();
           
        }

对于少于 160 个字符的短信,使用以下代码可以正常工作:

 try
                {

                SmsManager.Default.SendTextMessage("**MY NUMBER**", null, "test message", null, null);
                }
                catch (Exception ex)
                {                    

                    Console.WriteLine(ex.Message);
                }
  • 更改字符以将消息分成 160 到 150 个部分(不确定为什么它不适用于 153-160 之间的任何内容)

感谢Jason的帮助

  • 在上面的代码中,它缺少最后几个字符,因为如果您将字符串分成几部分,那么最后一部分字符可能较少,而不是恰好 150 个。更改了代码并添加了 trycatch语句获取不同部分的所有字符
void TestButton_Click (object sender, System.EventArgs e)
        {
            string message = editTextTx.Text;            

            if (((decimal)message.Length/ 150) == message.Length / 150)
                text_i = message.Length / 150;
            else
                text_i = (message.Length / 150) + 1;

            Android.App.AlertDialog.Builder dialog = new Android.App.AlertDialog.Builder(this);
            Android.App.AlertDialog alert = dialog.Create();
            alert.SetTitle("Warning");
            alert.SetMessage("It will need " + text_i.ToString() + " text message(s)");
            alert.SetButton("OK", (c, ev) =>
            {
var destinationAdd = "MY NUMBER";

                SmsManager sm = SmsManager.Default;
                if (message.Length >= 150)
                {
                    

                    //split the message into parts of 150 chars.

                    
                    List<string> parts = new List<string>();

                    foreach (int i in Enumerable.Range(0, text_i))
                    {
                        string tword = "";
                        try
                        {
                            tword = message[(i * 150)..][..150];

                        }

                        catch (Exception ex) //to get the last few characters
                        {
                            tword = message[(i * 150)..];
                        }
                        parts.Add(tword);                 
                                                

                    };

                    sm.SendMultipartTextMessage(destinationAdd, null, parts, null, null);
                }
                else
                {
                    sm.SendTextMessage(destinationAdd, null, message, null, null);
                }
});
            alert.Show();
           
        }