Reputation: 53
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();
}
Upvotes: 0
Views: 4628
Reputation: 2578
Putting aside your application seems like a spam bot, here is how I would implement it.
Using a for
or while
loop is probably what you are looking for.
(Where noSends
is the amount of times you wish to send the message)
public void sendMultiple(int noSends){
String phoneNo = phoneInput.getText().toString();
String sms = textSMS.getText().toString();
try
{
SmsManager smsManager = SmsManager.getDefault();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
for(int i = noSends; i != 0; i--)
{
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
}
if( prefs.getBoolean("addvalues", true) )
{
ContentValues values = new ContentValues();
values.put("address", phoneNo);
values.put("body", sms);
while(noSends != 0)
{
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
noSends--;
}
}
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(),getString(R.string.messageNotSent), Toast.LENGTH_LONG).show();
e.printStackTrace();
{
else
{
Toast.makeText(getApplicationContext(),getString(R.string.sentMessages), Toast.LENGTH_LONG).show();
}
}
Upvotes: 1