Send failures do not throw exceptions, they are reported differently.
If you look at the method's signature sendMultipartTextMessage you will see that it receives two Arrays<PendingIntent>
, sentIntents, and deliveryIntents .
public void sendMultipartTextMessage (String destinationAddress,
String scAddress,
ArrayList<String> parts,
ArrayList<PendingIntent> sentIntents,
ArrayList<PendingIntent> deliveryIntents)
They are used to notify the sending and delivery status of each part of the MultipartTextMessage .
Start by creating two BroadcastReceiver , one to receive notification of the sending status and another to receive notification of the delivery status.
SentReceiver.java
public class SentReceiver extends BroadcastReceiver {
private static SentReceiver instance;
private SentReceiver(){
}
public static SentReceiver getInstance(){
if(instance == null)instance = new SentReceiver();
return instance;
}
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()){
case Activity.RESULT_OK:
Toast toast = Toast.makeText(context, "SMS(parte) enviado",Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
toast = Toast.makeText(context, "Generic failure",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
toast = Toast.makeText(context, "No service", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
toast = Toast.makeText(context, "Null PDU", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
toast = Toast.makeText(context, "Radio off", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
}
}
}
DeliveredReceiver.java
public class DeliveredReceiver extends BroadcastReceiver {
private static DeliveredReceiver instance;
private DeliveredReceiver(){
}
public static DeliveredReceiver getInstance(){
if(instance == null)instance = new DeliveredReceiver();
return instance;
}
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast toast = Toast.makeText(context, "SMS(parte) entregue", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
case Activity.RESULT_CANCELED:
toast = Toast.makeText(context, "SMS(parte) não entregue", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
break;
}
}
}
In the enviaSMS()
method, create the Array<PendingIntent>
and pass them to the sendMultipartTextMessage()
method.
public void enviaSMS(Context context, String fone, String mensagem) {
SmsManager smsManager = SmsManager.getDefault();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<String> messages = smsManager.divideMessage(mensagem);
int messagesCount = messages.size();
//Criar os PendingIntent
PendingIntent piSent = PendingIntent.getBroadcast(context, 0,
new Intent("aSuaPackage.SMS_SENT"),
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent piDelivered = PendingIntent.getBroadcast(context, 0,
new Intent("aSuaPackage.SMS_DELIVERED"),
PendingIntent.FLAG_UPDATE_CURRENT);
//Preenche os arrays
for(int i=0; i<messagesCount; i++){
sentIntents.add(piSent);
deliveryIntents.add(piDelivered);
}
smsManager.sendMultipartTextMessage(fone, null, messages,
sentIntents, deliveryIntents);
}
To register your receivers use:
registerReceiver(SentReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_SENT"));
registerReceiver(DeliveredReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_DELIVERED"));