SMS Manager send mutlipart message when there is less than 160 characters
The message character limit depends on the character bit-size of the alphabet you're using. For the standard GSM 7-bit alphabet, the character limit is 160. For an 8-bit alphabet, it is 140, and for a 16-bit alphabet, which sounds like your situation, it is only 70 characters. If you must send messages with special Unicode characters, like those in non-Latin alphabets, then you're stuck with the 16-bit alphabet, and its 70-character limit. If you can somehow convert the messages to the basic 7-bit alphabet, you will have the 160-character limit.
How to send the SMS more than 160 character?
Junk characters? method sendMultipartTextMessage only send text message. If you want to send non text message, you should look to method sendDataMessage. Below is the code excerpt from android cts. It has example on how to send long messages.
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)
Send SMS with more than 160 characters using Xamarin.Android
- Changed characters for breaking the messages into parts from 160 to 150 (Not sure why it doesn't work for anything between 153-160)
Thanks Jason for the help
- In the above code it was missing the last few characters because if you break the string in to parts there can be last part with less chars and not exactly 150. Changed the code and added a
try
andcatch
statement to get the all the characters in different parts
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();
}
Android - Sending Many multi-part Messages will miss some of them
I tried to follow and use Mike M's very insightful guidance and code to write an SMS message sending service that enqueues (large or small) messages and waits for each message to be completely sent before sending the next one. Most of the real work is done in the background.
SmsService.java is a gist on GitHub because it's a bit large to paste here.
One of my goals was to see if I could put the necessary functionality into one single android component. It's been tested with small numbers of messages but it's still experimental. (e.g. it avoids using a BroadcastReceiver, relies on the uniqueness of Service startIds sent in by the system, heavily relies on intents to represent messages, etc.)
The class has three static methods for setting it up, sending messages, and tearing it down:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
SmsService.setup(this);
}
private void sendMyMessages() {
SmsService.send(this, recipient, sender, "the message text");
}
@Override
protected void onDestroy() {
SmsService.teardown(this);
...
}
sendMultipartTextMessage sending as multiple SMS messages?
When a message is long enough, typically more than 160 characters, it's sent as separate SMS messages, and on GSM networks a bit of extra meta data is added called a User Data Header (UDH) that tells the receiver that the separate messages should be combined.
What you want to happen is for the receiver to combine them back into a single message. Note that it's the receiver that combines them, so that's where you need to be looking. As far as I know, the behaviour of sendMultipartTextMessage in Lollipop is the same as in KitKat.
How to handle sms with more than 160 characters like 250 characters in broadcast receiver
Finally i done it thanx mr AxelH for your answer
DataBaseHandler db;
String mySmsText;
public void onReceive(Context context, Intent intent) {
db = new DataBaseHandler(context);
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (messages.length > 0) {
StringBuffer content = new StringBuffer();
for (SmsMessage sms : messages)
content.append(sms.getDisplayMessageBody());
mySmsText = content.toString();
}
db.update_sys_pwd(mySmsText);
instance.t1.setText(mySmsText);
}
}
Related Topics
Android Sd Card Write Permission Using Saf (Storage Access Framework)
Differencebetween Src and Background of Imageview
Smoothscrolltopositionfromtop() Is Not Always Working Like It Should
Map View Draw Directions Using Google Directions API - Decoding Polylines
How to Change Separator Color in Navigationview
How to Get References for All Currently Active Fragments in an Activity
Identifying Rtl Language in Android
How to Draw Road Directions Between Two Geocodes in Android Google Map V2
How to Display Border to Imageview
Android E/Parcel﹕ Class Not Found When Unmarshalling (Only on Samsung Tab3)
How to Change Package Name in Android Studio
Android Circular Determinate Progressbar
Equivalent of Clean & Build in Android Studio
Com.Google.Android.Gsf Package Couldn't Be Found
How to Do Circular Scrolling on Viewpager
Pass List of Objects from One Activity to Other Activity in Android