I want to send SMS messages to more than 100 people using my android app. The numbers are preloaded in a numbers.txt file from the assets folder.
So far, the app works by sending upto 10 SMS using a for...loop statement below. But that is where the app stops...
I have read online that SMS platforms usually send their messages on delay mode and I was advised to include a delay of 1000ms in my code.
Please help me on how to execute this for..statement in delay mode.
Below is my example.
EditText numInput = (EditText) findViewById(R.id.editTextPhone);
EditText msg = (EditText) findViewById(R.id.editMsg);
public int smsCount;
String phoneNumbers = numInput.getText.toString();
String message = msg.getText.toString();
String[] eachNumber = phoneNumbers.split(";");
smsCount = 0;
for(String num:eachNumber){
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(num, null, message, null, null);
smsCount = smsCount + 1;
Toast.makeText(getApplicationContext(), smsCount+" SMS Sent!", Toast.LENGTH_LONG).show();
}
The phoneNumber variable has upto 100 numbers. But the for...loop only runs upto 10 counts and stops there....
I want to add a delay after each message and then, send all the message irrespective of the number of recipients.
I have solved the problem... since I could not find any responsible help, I had to read up in Understanding and Utilizing Threads in Android in sitepoint.com.
However, all I needed to make the sendMessage() thread work was just to add
Thread.sleep(3000);
So, here is the complete sendMessage() thread.
public void onSendClick(View view) {
//checkRecipients();
//countSMSChars();
if(message.length() < 5){
Toast.makeText(getBaseContext(), "Invalid message format/length.", Toast.LENGTH_SHORT).show();
Log.d("Message: ",""+"Invalid message format/length: "+message.length());
}else if(charLength > maxLen ){
Toast.makeText(getApplicationContext(), "Maximum allowed characters is: "+maxLen, Toast.LENGTH_LONG).show();
}else{SENDMESSAGE();}
}
public void SENDMESSAGE(){
sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure - Please check your delimiter settings or check your account balance.",
Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(DELIVERED));
try{
runSendMessage();
}catch (Exception e){
Log.d("Error: ", ""+e);
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
}
}
public void runSendMessage(){
try{
checkDelimiterMode();//check the array split delimiter symbol
message = input.getText().toString(); //get the message to be sent
numbers = phoneNumbers.getText().toString();//get the recipients' numbers
//split the numbers array using the delimiter
String[] eachNumber = numbers.split(delimiterValue);
smsCount = 0; SMSAmount = 4.00;
for(String num:eachNumber){
Thread.sleep(3000);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(num, null, message, sentPI, deliveredPI);
smsCount = smsCount+1; pb.incrementProgressBy((eachNumber.length*100)/100);
Toast.makeText(this,smsCount+" SMS sent @ N"+SMSAmount+" = N"+(SMSAmount*smsCount) , Toast.LENGTH_LONG).show();
Log.d("Number of SMS sent...",""+smsCount+" @ N"+SMSAmount+" = N"+(SMSAmount*smsCount));
}
}catch (Exception e){
Log.d("Error: ", ""+e);
Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
}
}
And this code, can send more than a million SMS messages... sending each message after 3 seconds.