Falhas de envio não lançam excepção, elas são reportadas de outra forma.
Se olhar para a assinatura do método sendMultipartTextMessage verá que ele recebe dois Arrays<PendingIntent>
, sentIntents e deliveryIntents.
public void sendMultipartTextMessage (String destinationAddress,
String scAddress,
ArrayList<String> parts,
ArrayList<PendingIntent> sentIntents,
ArrayList<PendingIntent> deliveryIntents)
Eles(PendingIntent) são utilizados para notificar o estado de envio e entrega de cada parte da MultipartTextMessage.
Comece por criar dois BroadcastReceiver, um para receber a notificação do estado de envio e outro para receber a notificação do estado de entrega.
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;
}
}
}
No método enviaSMS()
crie os Array<PendingIntent>
e passe-os ao método sendMultipartTextMessage()
.
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);
}
Para registar os receivers use:
registerReceiver(SentReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_SENT"));
registerReceiver(DeliveredReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_DELIVERED"));