Shipping failures do not launch exception, they are reported otherwise.
If you look at the signature of the method sendMultipartTextMessage you’ll see he gets two Arrays<PendingIntent>
, sentIntents and deliveryIntents.
public void sendMultipartTextMessage (String destinationAddress,
String scAddress,
ArrayList<String> parts,
ArrayList<PendingIntent> sentIntents,
ArrayList<PendingIntent> deliveryIntents)
They(Pendingintent) are used to notify the shipping and delivery status of each part of the Multiparttextmessage.
Start by creating two Broadcastreceiver, one to receive notification of the sending state and one to receive notification of the delivery state.
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 method enviaSMS()
create the Array<PendingIntent>
and pass them on to the method 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);
}
To record the receivers use:
registerReceiver(SentReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_SENT"));
registerReceiver(DeliveredReceiver.getInstance(), new IntentFilter("aSuaPackage.SMS_DELIVERED"));
Check the log in Logcat that an error message should appear
– Giancarlo Abel Giulian
Had already checked, the method only returns Exception if the phone number is null or empty, and the message the same thing, but does not return the status of the send, if it was sent, if it was error, etc... there is something that can be done?
– Geferson