Search code examples
javaandroidarraylistsmsmanager

ArrayList clearing issue


I'm creating an android app that sends sms. I actually have issues with the ArrayList containing the PendingIntents (SentPenIntents and DelPenIntents), which contains the succes or fail of the sending operation.

    private Runnable seekSms = new Runnable() {
    @Override
    public void run() {
        try {
            if(phoneHasID) {
                try { 

                        ArrayList<PendingIntent> SentPenIntents = new ArrayList<>();
                        ArrayList<PendingIntent> DelPenIntents = new ArrayList<>();                      
                        String SENT_SMS_FLAG = "SENT_SMS";
                        String DELIVER_SMS_FLAG = "DELIVER_SMS";

                        Intent sentIn = new Intent(SENT_SMS_FLAG);
                        PendingIntent sentPIn = PendingIntent.getBroadcast(getApplicationContext().getApplicationContext(),0,sentIn,0);

                        Intent deliverIn = new Intent(DELIVER_SMS_FLAG);
                        PendingIntent deliverPIn = PendingIntent.getBroadcast(getApplicationContext().getApplicationContext(),0,deliverIn,0);

                        BroadcastReceiver sentReceiver = new BroadcastReceiver(){
                            @Override public void onReceive(Context c, Intent in) {
                                switch(getResultCode()){
                                    case Activity.RESULT_OK:
                                        System.out.println("successfully sent !!");
                                        break;
                                    default:
                                        System.out.println("sending failed !!");
                                        break;
                                }
                            }
                        };
                        BroadcastReceiver deliverReceiver = new BroadcastReceiver(){
                            @Override public void onReceive(Context c, Intent in) {
                                System.out.println("message received !!");

                            }
                        };
                        registerReceiver(sentReceiver, new IntentFilter(SENT_SMS_FLAG));
                        registerReceiver(deliverReceiver, new IntentFilter(DELIVER_SMS_FLAG));

                        SmsManager smsManager = SmsManager.getDefault();
                        ArrayList<String> smsParts = smsManager.divideMessage(sms);


                        for (int i=0; i < smsParts.size(); i++){
                            SentPenIntents.add(i, sentPIn);
                            DelPenIntents.add(i, deliverPIn);
                        }

                        smsManager.sendMultipartTextMessage(numero, null, smsParts, SentPenIntents, DelPenIntents);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        pauseSms.postDelayed(seekSms, delayCT * 1000);
    }
};

It all works, the first time : in my console, i get, for a 3 sms-long message :

09-29 15:57:56.137  15781-15781/? I/System.out﹕ successfully sent !!
09-29 15:57:56.652  15781-15781/? I/System.out﹕ successfully sent !!
09-29 15:57:57.051  15781-15781/? I/System.out﹕ successfully sent !!
09-29 15:57:58.753  15781-15781/? I/System.out﹕ message received !!
09-29 15:57:59.673  15781-15781/? I/System.out﹕ message received !!
09-29 15:58:16.272  15781-15781/? I/System.out﹕ message received !!

when the app comes to the sending of the second message, i get 3 more output messages for each list :

09-29 16:03:47.603  16011-16011/? I/System.out﹕ successfully sent !!
09-29 16:03:47.604  16011-16011/? I/System.out﹕ successfully sent !!
09-29 16:03:48.028  16011-16011/? I/System.out﹕ successfully sent !!
09-29 16:03:48.028  16011-16011/? I/System.out﹕ successfully sent !!
09-29 16:03:51.093  16011-16011/? I/System.out﹕ message received !!
09-29 16:03:51.095  16011-16011/? I/System.out﹕ message received !!
09-29 16:03:51.310  16011-16011/? I/System.out﹕ message received !!
09-29 16:03:51.310  16011-16011/? I/System.out﹕ message received !!
09-29 16:04:03.299  16011-16011/? I/System.out﹕ successfully sent !!
09-29 16:04:03.299  16011-16011/? I/System.out﹕ successfully sent !!
09-29 16:04:05.744  16011-16011/? I/System.out﹕ message received !!
09-29 16:04:05.744  16011-16011/? I/System.out﹕ message received !!

and so on, the list stacks. I might have a problem clearing my ArrayList but i cant find out what it is.


Solution

  • You are never removing stuff from your lists. This is why it is stacking.

    Try and remove intents when you receive a delivery confirmation.

    EDIT After further reviewing your code, I saw that you are registering the receivers each time you call your method. This leads to multiple receivers intercepting the events and printing the message only once. Try register them only once and see what happens.

    You can easily test this by calling unregisterReceiver at the end of your method.