其他分享
首页 > 其他分享> > android – 发送导致问题的50条消息会导致一般性故障

android – 发送导致问题的50条消息会导致一般性故障

作者:互联网

我正在开发一个应用程序,我需要发送100条消息.经过几个线程后我才知道发送邮件有限制,可以在一小时内发送100封邮件.为此,我将收件人列表分成块,并在每个块之间放置5秒的延迟,并在每条消息中放置3秒的延迟.块之间的延迟在每个块之后增加,当它达到100秒时,它将重置为5秒.之后它可以正常工作50条消息,但是当我提出收件人列表时,它会导致问题,一些消息没有首先出现,并显示为本机错误消息.

有没有任何标准的方法来实现这一点我可能需要发送100条消息,如何一次发送多条消息而没有任何失败.如果我需要延迟应该是块或消息之间的适当延迟.

提前致谢.

private final int MAX_SMS_IN_ONE_TIME = 10;
private final int DELAY_BETWEEN_CHUNKS = 5000;

public void sendMessage(arguments){ 
    // Send long messages in chunk of 20 messages and put gap of increasing 5 seconds till 50 seconds and then reset.

    final Iterator iterator = messageChunks.iterator();
    new Thread(new Runnable() {
        @Override
        public void run(){

        int interval =1;
        while (iterator.hasNext()) {

            for (final Contact contact :
                (List<Contact>) iterator.next()) {

                sendSMS(body, contact.getmMobileNumbers().get(0));

                App.trackEvent("Message", "Sent", "Messages from our sms app");
            }
        }
        try {
            Log.i("chunk", "chunk # " + interval + " delay is " + DELAY_BETWEEN_CHUNKS);
            Thread.sleep(DELAY_BETWEEN_CHUNKS * interval);
            interval++;
            if (interval == 10) {
                interval = 1;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        }
        }
    }).start();
}

public void sendSMS(final String message, final String phoneNo) {
    try {
        String SENT = "com.ebryx.smscustommessagegeneration"+""+System.currentTimeMillis()+""+((int)this.getmMessageId());
        Intent intentMessageASendStatus = new Intent(SENT);

        final PendingIntent pi = PendingIntent.getBroadcast(App.getContext(),  ((int)this.getmMessageId()),
                intentMessageASendStatus, PendingIntent.FLAG_CANCEL_CURRENT);
        final ArrayList<PendingIntent> sentPI = new ArrayList<PendingIntent>(){{add(pi);}};

        App.getContext().registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {


                switch (getResultCode())
                {
                    case Activity.RESULT_OK:

                        Log.i("tag","sent successfully ");
            break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:

                        Log.i("tag","Generic Failure");
                break;

                    case SmsManager.RESULT_ERROR_NO_SERVICE:

                        Log.i("tag","No service failure");
            break;

        case SmsManager.RESULT_ERROR_NULL_PDU:
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:

                        Log.i("tag","Airplane mode failure");
                        break;
                }
            }
        }, new IntentFilter(SENT));

        final SmsManager smsManager = SmsManager.getDefault();
        final ArrayList<String> parts = smsManager.divideMessage(message);

        new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    smsManager.sendMultipartTextMessage(phoneNo, null, parts, sentPI, null);

                }}, 3000);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

解决方法:

你应该使用两个具有SENT和DELIVER未决意图的广播接收器.

消息传递后,您必须添加回调机制以通知已经传递的消息和stat发送新消息.此呼叫应该同步.

>创建一个HashMap,并根据我们在下面的传送广播接收器中获得的传送状态逐个发送到下面的方法.

    /**
     * Sends an SMS message to another device
     * 
     * @param phoneNumber Number to which msg send
     * @param message Text message
     */
private void sendSMS(String phoneNumber, String message) {
    // Intent Filter Tags for SMS SEND and DELIVER
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
// STEP-1___
    // SEND PendingIntent
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
            SENT), 0);

    // DELIVER PendingIntent
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);
// STEP-2___
    // SEND BroadcastReceiver
    BroadcastReceiver sendSMS = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(getBaseContext(), "SMS sent",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(getBaseContext(), "Radio off",                            Toast.LENGTH_SHORT).show();
                break;
            }
        }
    };

    // DELIVERY BroadcastReceiver
    BroadcastReceiver deliverSMS = new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                 // TODO : notify from here to send new message.
                 // Add callback mechanism 
                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;
            }
        }
    };
// STEP-3___
    // ---Notify when the SMS has been sent---
    registerReceiver(sendSMS, new IntentFilter(SENT));

    // ---Notify when the SMS has been delivered---
    registerReceiver(deliverSMS, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
 }
}

>您还可以在SMS交付时从HashMap中删除元素
这有助于您跟踪成功传递的邮件数量.

标签:android,sms,smsmanager
来源: https://codeday.me/bug/20190522/1153782.html