Save SMS to inbox when sending text using Sms Manager Android - 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!

Related

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

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.

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.

Send SMS failed via Android Device

I am writing an app in which i am trying to send SMS to a Recepient, but whenever i do click on Send, getting message:- SMS faild, please try again later!
Either i am using Emulator or Android Device....
Like you can see in below screen shot, here i am trying to send message to Pratik, which is saved in my Phonebook Contacts, but whenever i am trying to message to Pratik not able to send message to Pratik
Please see below screen shot, like you can see, here i am trying to send message to Rahul...
Manifest.xml:
<uses-permission android:name="android.permission.SEND_SMS" />
Please check below code:
private TextView name;
private ListView list;
private Database db;
private Contact contact;
ImageButton buttonSend;
EditText textSMS;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editor);
// bind GUI components
this.name = (TextView) findViewById(R.id.editor_name);
this.list = (ListView) findViewById(R.id.editor_list);
// check if contact id is valid
this.db = new Database(getContentResolver());
int contactId = getIntent().getIntExtra(CONTACT_ID, NO_CONTACT_ID);
this.contact = this.db.getContact(contactId);
if (this.contact == null) {
finish();
}
this.name.setText(this.contact.getName());
// pre-load information about all account types
AuthenticatorDescription[] authTypes = AccountManager.get(this).getAuthenticatorTypes();
for (AuthenticatorDescription authDesc : authTypes) {
this.map.put(authDesc.type, authDesc);
}
// bind list events
this.list.setOnItemClickListener(this);
this.list.setOnCreateContextMenuListener(this);
// create the GUI
updateView();
saveGreeting = (ImageButton) findViewById(R.id.greeting);
saveGreeting.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
customGreeting(v);
}
});
buttonSend = (ImageButton) findViewById(R.id.buttonSend);
textSMS = (EditText) findViewById(R.id.editTextSMS);
buttonSend.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String sms = textSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(CONTACT_ID, null, sms, 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();
}
}
});
}
Do not use recipient name to send message , use mobile number to send message .
Get contact number from contact list and use that contact number instead of name in sendTextMessage function.

Categories

Resources