How to send more than 160 characters SMS in android? [duplicate] - android

This question already has answers here:
How to send the SMS more than 160 character?
(4 answers)
Closed 6 years ago.
I have some code to send SMS from my application but this code only send 160 characters and cannot send more than 160. This is my code :
protected void sendMessage(String message){
String phoneNumber = "xxxx";
try {
if(message.length() < 161){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS Send !", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Character too long !", Toast.LENGTH_LONG).show();
}
}catch (Exception e){
Toast.makeText(getApplicationContext(), "SMS Failed !", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
How to send SMS with more than 160 characters ?

Send like this without worrying of its size.
protected void sendMessage(String message) {
try {
String phoneNumber = "xxxx";
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
//smsManager.sendTextMessage(phoneNumber, null, message, null, null);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts,
null, null);
Toast.makeText(getApplicationContext(), "SMS Send !", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "SMS Failed !", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}

Send the message over 2 texts. SMS has a limit of 160.

Related

How to send my location with text message?

Only text message is able to send through this code but I don't know how to code for to send current location link with text message
case R.id.button7:
Intent i1 = new Intent(Intent.ACTION_CALL);
i1.setData(Uri.parse("tel: 700000000"));
startActivity(i1);
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("70000000", null, "hello", null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}}
If you want to add it to the SMS message you need a way to recover it from the rest of the message.
Just define a separator character that will never be used on the text.
Example:
"hello|3.3452|22.2323"
Once you receive that string on the other side you can use
String.split("|");
That will return an array containing this:
[0]"hello"
[1]"3.3452"
[2]"22.2323"
Just knowing that Lat will be always at position 1 Long at 2 and the text message at 0
Hope this helps.
To send user's location as TextMessage you need to get Latitude and Longitude of the phone, you can do that by.
GPSTracker mGps = new GPSTracker(MainActivity.this);
double latitude = mGps.getLatitude();
double longitude = mGps.getLongitude();
Now to send it as a TextMessage, you need to use SmsManager. Use this function in your button press.
protected void sendSMSMessage() {
Log.i("Send SMS", "");
double latitude = mGps.getLatitude();
double longitude = mGps.getLongitude();
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
String phoneNo = mTxtphoneNo.getText().toString();//get phone number from the textView of the editText or wherever you have feeded the number
String message = "These are my co-ordinates:-" + latitude + ", "
+ longitude + "\nAddress:-";
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(
getApplicationContext(),
"SMS sent with current latitude and logitude",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}

what is the method for sending SMS in android?

I'm trying to send a text message and my code ain't working. Can anyone help me out with that?
I tried using code given in tutorials point but its not working.
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo,null,message,null,null);
Toast.makeText(getApplicationContext(), "SMS sent.",Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"SMS faild.",Toast.LENGTH_LONG).show(); e.printStackTrace(); }
}
Try like this:
private void sendSMS(String phoneNumber, String message)
{
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
Also check this Sending a SMS Message from an Android Application
It's SMSManager.sendTextMessage. Remember to request permission in your manifest.

Android - Sending an SMS on button click.

So I am trying to make my app send an SMS automatically to the given number when the user presses the button.
I can make it open the messenger and write the text but I can't make it send it automatically.
My code is as follows (The part that matters I guess);
#Override
public void onClick(View a) {
if(a.equals(sms)){
tekst = (TextView) findViewById(R.id.txt);
Uri tlf = Uri.parse("smsto:"+tekst.getText().toString());
Intent c = new Intent(Intent.ACTION_VIEW, tlf);
c.setData(tlf);
c.putExtra("sms_body","Hjelp jeg er i fare!" );
startActivity(c);
}else{
tekst = (TextView) findViewById(R.id.txt);
Intent c = new Intent(Intent.ACTION_CALL);
Uri tlf = Uri.parse("tel:"+tekst.getText().toString());
c.setData(tlf);
startActivity(c);
}
}
So, how can I make it send the SMS?
BTW, I have added the permission: "android.permission.SEND_SMS"
Try with this:
String phoneNumber = "<phone_number_here>";
String message = "Test Message";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Notice that it's a very simple snip code that you can implement more.
If you want to see SMS showing up in any of the other SMS Clients/Apps installed yuo must use:
ContentValues values = new ContentValues();
values.put("address", "<phone_number_here>");
values.put("body", "Test Message");
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
and add:
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
try this
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage([number], null, [sms], null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
where [number] is the number to which you want to send your sms and [sms] is your text you wanna send
Try this one.
//Declare the button and the tetviews to input number and the message
Button sendSMSBtnBtn = (Button) findViewById(R.id.btnSendSMS);
txtphoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
txtMessage = (EditText) findViewById(R.id.editTextSMS);
//Calling the method sendSMSMessage in the button click event
sendSMSBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sendSMSMessage();
}
});}
// Method to send SMS using SMS Manager
protected void sendSMSMessage() {
Log.i("Send SMS", "");
String phoneNo = txtphoneNo.getText().toString();
String message = txtMessage.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
Note : Make sure you have the set the following permission in the Manifest file.
<uses-permission android:name="android.permission.SEND_SMS" />

Sending Encoded SMS using SmsManager does not get received

When in send direct SMS there is no problem but when I send operational SMS that contains DNA bases(A , G , T , C only) then it is not working.
Plaintext is normal message. Whats the problem?? Please help.
public class sendMessage extends Activity {
Button button;
EditText plainTxt;
EditText cipherText;
EditText editPhoneNum;
int plaintxtArray[] = new int[1500];
Bundle bundle=new Bundle();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smssend);
button = (Button) findViewById(R.id.button);
editPhoneNum = (EditText)findViewById(R.id.editPhoneNum);
plainTxt = (EditText) findViewById(R.id.editSMS);
cipherText = (EditText)findViewById(R.id.editcipher);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String phoneNo = editPhoneNum.getText().toString();
//Toast.makeText(getBaseContext(), "Number is: " + phoneNo, Toast.LENGTH_LONG).show();
String plainText = plainTxt.getText().toString();
String CipherText=DNAbaseConvert(plainText);
Toast.makeText(getBaseContext(), "Cypher Text is: " + CipherText, Toast.LENGTH_LONG).show();
MessageToSent( phoneNo, CipherText);
}
});
}
public String DNAbaseConvert(String plainText)
{
//various operation goes here.
return b; //b-> a string , length 7-8 charecter long.
}
public void MessageToSent(String phoneNo, String CipherText) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, CipherText, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public void onBackPressed() {
super.onBackPressed();
Intent www = new Intent(sendMessage.this, LoggedIn1.class);
startActivity(www);
finish();
}
}
You can try this:
try {
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(CipherText);
smsManager.sendMultipartTextMessage(phoneNo, null, parts, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
For more help you can see this thread
Try saving the response from DNAbaseConvert(plainText) in a variable and pass it to sendTextMessage()
String msg=DNAbaseConvert(plainText);
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
This is because the response from DNAbaseConvert() may caused problem inside it..
You might be having a problem hitting the SMS message size limit. If you're using the SmsManager.sendTextMessage() method, you might instead try the SmsManager.sendMultipartTextMessage() method, with the SmsManager.divideMessage() method to split up your string.

Save SMS to inbox when sending text using Sms Manager Android

I had a bit of code that i used to send an sms message to a number that the user entered by pressing a button. However, when the message sends, it doesn't show up in their messaging inbox so they don't know if it exactly sent or not. Is there any way i could alter the text below to save all the sms messages to the users inbox? Thanks!
This is my code:
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String phoneNo = textPhoneNo.getText().toString();
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "Message Sent!", Toast.LENGTH_LONG).show();}
catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Unable to send message",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
Add this method to your class
private void addMessageToSent(String telNumber, String messageBody) {
ContentValues sentSms = new ContentValues();
sentSms.put("address", telNumber);
sentSms.put("body", messageBody);
ContentResolver contentResolver = getContentResolver();
contentResolver.insert(Uri.parse("content://sms/sent"), sentSms);
}
Hope it helps!

Categories

Resources