sendMultipartTextMessage并检查android eclipse中发送的报告问题

时间:2013-09-08 10:15:43

标签: android eclipse sms

我正在制作一个应用程序,将sendMultipartTextMessage发送给许多联系人。应用程序下载更新列表并将其发送给联系人。主要问题是我想在发送以前的更新后再次检查新的更新。我想检查以前的邮件是否发送给所有收件人。因此,应用程序将下载新列表并再次发送。但我注意到的主要问题是多部分SMS在发送每个部分后发送其发送报告。并且还有“case Activity.RESULT_OK:”事件多次触发,我的意思是超过TextMessages和部分的长度。这是我正在使用的代码,从一些网站复制。

    private void sendSMS(String phoneNumber, String message)
        {        
            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(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                    case Activity.RESULT_OK:
                        System.out.println("SMS SENT");
// msgcounter = length of recipients I want to send messages. 
                        msgcounter = msgcounter -1;
                        if (msgcounter < 0){
                                if (StartInfo){
                                    new RequestTask().execute(BaseUrl);
                                }
                        }else{
                            CreatMsg();
                        }
                        break;
                    }
                }
            }, new IntentFilter(SENT));

            //---when the SMS has been delivered---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, 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;                        
                    }
                }
            }, new IntentFilter(DELIVERED));        

            SmsManager sms = SmsManager.getDefault();
            ArrayList<String> parts = sms.divideMessage(message);
            ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
            ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
            for (int number = 0; number < parts.size(); number++) { 
                    sentIntents.add(sentPI);
                    deliveryIntents.add(deliveredPI);

            }
            sms.sendMultipartTextMessage(phoneNumber, null, parts,sentIntents, deliveryIntents);    
        }

1 个答案:

答案 0 :(得分:0)

以防万一其他人正在寻找相同的解决方案,这是因为registerReceiver被多次调用了,因此报告的处理与接收者注册的次数一样多。解决方法是将registerReceiversendSMS中移出,并且只调用一次。