I tried the code mentioned in the accepted answer for Send SMS with more than 160 characters
This doesn't work as expected and no text messages are sent after I tap 'Ok' in the display alert message. I couldn't find any error, not sure what happens in the background. I don't have enough reputation to comment on this.
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();
}
It works fine for one text message which has less than 160 characters with the below code:
try
{
SmsManager.Default.SendTextMessage("**MY NUMBER**", null, "test message", null, null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Thanks Jason for the help
try
and catch
statement to get the all the characters in different partsvoid 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();
}