Sunday, 11 August 2013

Send multiple sms in Android

Send multiple sms in Android

I was making an app that could send multiple sms messages (all with the
same text) to one recipient. Currently, the app has a pretty poor set up
for sending 5 messages at once.
Here is what I used to send 5 sms messages. Is there a better way to send
multiple messages, and a better way to store the messages in the users
inbox? Because copying and pasting the same thing over and over is really
messy. Thanks!
public void function1(int id){
String phoneNo = phoneInput.getText().toString();
String sms = textSMS.getText().toString();
try {
Toast.makeText(getApplicationContext(),getString(R.string.sentMessages),
Toast.LENGTH_LONG).show();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
final boolean save = prefs.getBoolean("addvalues", true);
if(save){
ContentValues values = new ContentValues();
values.put("address", phoneNo);
values.put("body", sms);
getContentResolver().insert(Uri.parse("content://sms/sent"),
values);
getContentResolver().insert(Uri.parse("content://sms/sent"),
values);
getContentResolver().insert(Uri.parse("content://sms/sent"),
values);
getContentResolver().insert(Uri.parse("content://sms/sent"),
values);
getContentResolver().insert(Uri.parse("content://sms/sent"),
values);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),getString(R.string.messageNotSent),
Toast.LENGTH_LONG).show();
e.printStackTrace();
}

No comments:

Post a Comment