ArrayList清除问题

时间:2015-09-29 14:15:27

标签: java android arraylist smsmanager

我正在创建一个发送短信的Android应用。实际上,我ArrayList包含PendingIntentsSentPenIntentsDelPenIntents)的问题,其中包含发送操作的成功或失败。

    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);
    }
};

一切正常,第一次:在我的控制台中,我得到了一条长达3短的消息:

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 !!

当应用程序发送第二条消息时,我为每个列表再输出3条消息:

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 !!

等等,列表堆栈。我可能在清除ArrayList时遇到问题,但我无法找出它是什么。

1 个答案:

答案 0 :(得分:0)

您永远不会从列表中删除内容。这就是堆叠的原因。

收到送货确认信息后,请尝试删除意图。

修改 在进一步检查您的代码后,我看到您每次调用方法时都在注册接收器。这导致多个接收器拦截事件并仅打印消息一次。尝试只注册一次,看看会发生什么。

您可以通过在方法结束时调用unregisterReceiver来轻松测试。