简体   繁体   中英

How to find out which sms has not been delivered or sent?

I have following code and I want to know which sms has been delivered or not. My code sends 1-5 sms every 30 seconds, so when toast "sms not delivered" appears, I do not know which one was not delivered. I don't know if this is the right way to do this but It was the most common solution to this problem

public boolean sendSMS(String id, String num, String msg) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

    registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

    registerReceiver(deliveryBroadcastReciever, new 
 IntentFilter(DELIVERED));
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage("xxxxxxxx", null, msg, sentPI, deliveredPI);

    Log.e("Message Sent", num + "  " + msg + "  " + id);

    return true;
}


class DeliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {


        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "sms delivered",
                        Toast.LENGTH_SHORT).show();
                break;
            case Activity.RESULT_CANCELED:
                Toast.makeText(getBaseContext(), "sms not delivered",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }
}

class SentReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "sms sent", Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU", 
      Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                break;
        }

    }

I'm sorry for my bad English.

EDIT:

 public boolean sendSMS(String id, String num, String msg) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);

    registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

    registerReceiver(deliveryBroadcastReciever, new IntentFilter(DELIVERED));

    smsManager.sendTextMessage("+xxxxxxxxxx", null, msg, sentPI, deliveredPI);

    Log.e("Message Sent", num + "  " + msg + "  " + id);

    return true;
}


public class DeliverReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        final Bundle bundle = intent.getExtras();

        try {

            if (bundle != null){
                final Object[] bObject = (Object[]) bundle.get("pdus");

                for (int i = 0; i < bObject.length; i++){

                    SmsMessage current = SmsMessage.createFromPdu((byte[]) bObject[i]);
                    String phoneNum = current.getDisplayOriginatingAddress();
                }
            }
        }catch (Exception e){
            Log.e("Deliver Reciever",e.toString());
        }
    }
}

Debug screenshot: http://imgur.com/a/GulgP

Firstly, make your SmSManager a static class object. Then you can get the current phone number/phone numbers from the PDU that is hiddent inside intent object.

final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {
    final Bundle bundle = intent.getExtras();
    String phone = "";
    SmsMessage smsMessage;

    try {

        if (bundle != null) {

            final Object[] pdu = (Object[]) bundle.get("pdu");

            for (int i = 0; i < pdu.length; i++) {

                 smsMessage = SmsMessage.createFromPdu((byte[]) pdu[i]);
                 phone = current.getDisplayOriginatingAddress();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM