M
M
math_man2017-02-25 10:35:48
Android
math_man, 2017-02-25 10:35:48

How to catch SMS delivery reports Android?

If I send 100 messages in a row in a cycle, then I will start receiving delivery reports. How do I know which of the recipients actually received the number and which did not?
Maybe you need to make changes to the code snippet?

public void sendSMS(String phone, String text, final String id){
        String SENT="SMS_SENT";
        String DELIVERED="SMS_DELIVERED";
        PendingIntent sentPI= PendingIntent.getBroadcast(getBaseContext(),0,
                new Intent(SENT),0);

        PendingIntent deliveredPI= PendingIntent.getBroadcast(getBaseContext(),0,
                new Intent(DELIVERED),0);
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1){
                switch(getResultCode())
                {
                    case Activity.RESULT_OK:
                        setStatus(id, "2");
                        Account.sent++;
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        break;
                }
            }
        },new IntentFilter(SENT));
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1){
                switch(getResultCode())
                {
                    case Activity.RESULT_OK:{
                        setStatus(id, "3");
                        Account.get++;
                        //Account.list.remove(this);
                        break;
                    }

                    case Activity.RESULT_CANCELED:{
                        setStatus(id, "4");
                        Account.notGet++;
                        //Account.list.remove(this);
                        break;
                    }

                }
            }
        },new IntentFilter(DELIVERED));
        
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNo,null, message, sentPI, deliveredPI);
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rou1997, 2017-02-25
@math_man

Perhaps this one Intent arg1will contain a special extra with data, see the documentation, although it is unlikely. But even without this, the task is elementary, since you pass both sentPIs to sendTextMessage, then they will match, it remains only to transfer the field to the anonymous class, this is done like this:

final String num = ...;
...
        registerReceiver(new BroadcastReceiver(){
...
//переменная num доступна

The compiler will do the rest.
Another option is to create just an inner class instead of an anonymous class, add a parameter to the constructor.
Or the third, "put" an anonymous class in a variable, declare a public field in it, set this field, and only then registerReceiver.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question