2

我正在制作一个应用程序来将 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);    
        }
4

1 回答 1

1

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

于 2019-12-26T05:20:10.733 回答