Send Multiple sms in android

Send Multiple SMS to android

This is the sample code for sending multiple SMS at a time in android.using PendingInent and Broadcast Receiver.
Step 1
get all the contacts from the devices using the content provider.
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(Phone.CONTENT_URI, null, null, null,null+ " ASC");
while (phones.moveToNext()){
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.
DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(phoneNumber!=null && name!=null ){
if(phoneNumber.length()>0 && name.length()>0){
contact_names.add(name);
Log.d("NAME", name);
contact_numbers.add(phoneNumber);
Log.d("PHONE", phoneNumber.toString());
}
}

}
phones.close();
}

Step 2
Register Broadcast Receiver for sent and delivery SMS report.

registerReceiver(bReceiver, new IntentFilter(SENT));
registerReceiver(bReceiver2, new IntentFilter(DELIVERED));

public final BroadcastReceiver bReceiver2=new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
// showToastMessage("SMS delivered");
break;
case Activity.RESULT_CANCELED:
// showToastMessage("SMS not delivered");
break; 
}
}
};

public final BroadcastReceiver bReceiver =new BroadcastReceiver(){

@Override
public void onReceive(Context arg0, Intent arg1) {
// isNewSMSAction = true;
switch (getResultCode())
{
case Activity.RESULT_OK:
// showToastMessage("SMS sent");
restoreSms(smsObject,"sur.RESULT_ERROR_GENERIC_FAILURE:

// showToastMesccess");
break;
case SmsManagesage("Generic failure");
restoreSms(smsObject,"failed");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
// showToastMessage("No service");
restoreSms(smsObject,"success");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
// showToastMessage("Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
// showToastMessage("Radio off");
break;
default : 
break;
}
}
};
Step 3
Send SMS using Sms Manager and pendingIntent.
ArrayList sentPendingIntents = new ArrayList();
ArrayList deliveredPendingIntents = new ArrayList();
try {
SmsManager sms = SmsManager.getDefault();
ArrayList mSMSMessage = sms.divideMessage(message);
for (int i = 0; i < mSMSMessage.size(); i++) {
sentPendingIntents.add(i, sentPI);
deliveredPendingIntents.add(i, deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage,sentPendingIntents, deliveredPendingIntents);

} catch (Exception e) {

e.printStackTrace();
// Toast.makeText(context, "SMS sending failed...", Toast.LENGTH_SHORT).show();
}

Step 4
If you want to store sent messages into the messenger, you have to use following method in Broadcast Receiver while sending.

It will store sent messages in the inbox and failed messages in the draft.

public boolean restoreSms(Sms obj,String type) {
boolean ret = false;
try {
ContentValues values = new ContentValues();
values.put("address", obj.getAddress());
values.put("body", obj.getMsg());
values.put("read", obj.getReadState());

String storeLocation = "sent";
if(type.equalsIgnoreCase("success")){
storeLocation = "sent";
// values.put("status", 0);
}else{
storeLocation = "draft";
// values.put("status", 1);
}
values.put("date", obj.getTime());
this.getContentResolver().insert( Uri.parse("content://sms/"+storeLocation),values);
//Uri.parse("content://sms/inbox", values);
ret = true;
} catch (Exception ex) {
ret = false;
}
return ret;
}
Step 5
Finally, unregister the receiver in onDestroy method, using
unregisterReceiver(bReceiver);
unregisterReceiver(bReceiver2);

Note: Please add needed permission in the manifest.xml

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

<uses-permission android:name="android.permission.READ_SMS"/>

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<uses-permission android:name="android.permission.SEND_SMS"/>

<uses-permission android:name="android.permission.WRITE_SMS"/>

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Screenshot

Send Multiple sms

Download

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top