How do I know if there was a glitch in texting?

Asked

Viewed 205 times

3

I have this method of sending SMS via app:

public boolean enviaSMS(String fone, String mensagem) throws Exception {
    try {
        smsManager.sendMultipartTextMessage(fone, null,
                smsManager.divideMessage(mensagem), null, null);
        return true;
    } catch (Exception e) {
        return false;
    }
}

However, Android never returns exception if you cannot send, there is some way to get the exception when there was some failure in sending?

  • Check the log in Logcat that an error message should appear

  • 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?

1 answer

4


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"));
  • 1

    Just to increment the answer, never fall in the catch as "Try catch" is added to handle exceptions at runtime. In that case do not fall in the catch because there was no error of execution. And as the ramaral explained failed to add the pendingIntent to be notified of the sms delivery status systems.

  • @Alessandrobarreto Thanks for the note.

  • Thank you very much, that was exactly it, solved my problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.