I am using below code for sending sms in background for multiple peoples(Contacts) at a time. It is working for smaller text but however it not working for text which has more than 160 characters. I am getting GENERIC FAILURE.
public class SendSMS {
private int mMessageSentParts;
private int mMessageSentTotalParts;
private int mMessageSentCount;
private String message;
private String[] array;
private Context mContext;
public SendSMS(Context context,String array[]) {
// TODO Auto-generated constructor stub
this.mContext = context;
this.array = array;
System.out.println("array length :::: "+array.length);
message = mContext.getResources().getString(R.string.siri);
// message="siri";
startSendMessages();
}
private void startSendMessages() {
registerBroadCastReceivers();
mMessageSentCount = 0;
sendSMS(array[mMessageSentCount].toString(), message);
}
private void sendNextMessage() {
if (thereAreSmsToSend()) {
sendSMS(array[mMessageSentCount].toString(), message);
} else {
Toast.makeText(mContext, "All SMS have been sent",
Toast.LENGTH_SHORT).show();
}
}
private boolean thereAreSmsToSend() {
return mMessageSentCount < array.length;
}
private void sendSMS(final String phoneNumber, String message) {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
mMessageSentTotalParts = parts.size();
Log.i("Message Count", "Message Count: " + mMessageSentTotalParts);
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0,
new Intent(DELIVERED), 0);
for (int j = 0; j < mMessageSentTotalParts; j++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
mMessageSentParts = 0;
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
deliveryIntents);
}
private void registerBroadCastReceivers() {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
mContext.getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
mMessageSentParts++;
if (mMessageSentParts == mMessageSentTotalParts) {
mMessageSentCount++;
sendNextMessage();
}
Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(mContext, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(mContext, "No service", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(mContext, "Null PDU", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(mContext, "Radio off", Toast.LENGTH_SHORT)
.show();
break;
}
}
}, new IntentFilter(SENT));
mContext.getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(mContext, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
}
}
Its working fine in OS 4.2.2 but not working with OS 4.4.2 Can you please help me. Thank you guys.