发送短信编程到多的人越来越一般性错误(Sending SMS programmatically to

2019-07-17 16:06发布

我目前正在开发一个短信应用程序,允许用户发送短信给多人。

由于短信是长我必须使用sendMultipartTextMessage,低于

private void sendSMS(final String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(message);
    int messageCount = parts.size();

    Log.i("Message Count", "Message Count: " + messageCount);

    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    for (int j = 0; j < messageCount; j++) {
        sentIntents.add(sentPI);
        deliveryIntents.add(deliveredPI);
    }

    // ---when the SMS has been sent---
    registerReceiver(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;
            }
        }
    }, 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));

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

我会遇到一个通用的错误,当我试图将其发送给多个人在一个循环这样,消息将不被发送。 我怀疑这是因为消息可能是3-4零件和android系统不能及时甚至与3000毫秒延迟发送邮件。

    for (int t = 0; t < array.length; t++) {
        System.out.println("temp: " + array[t].toString());
        try {
            sendSMS(array[t].toString(), message);
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

编辑:上面的代码是在该服务内执行的的AsyncTask。

Answer 1:

IHMO您没有使用正确的方法。 我使用的是不同的一个,它是工作。 我试着解释:

你应该保持一个计数器所有挂起的意图,因为如果你发送短信主要有两个部分,您将收到两个RESULT_OK(或RESULT_ERROR_ *),只有当您收到结果,你应该继续发送下一个SMS各地。 我不喜欢阻塞线程的想法...也许这是你得到奇怪的错误的原因。

我重构你的代码我的方法:

private int mMessageSentParts;
private int mMessageSentTotalParts;
private int mMessageSentCount;

private void startSendMessages(){

    registerBroadCastReceivers();

    mMessageSentCount = 0;
    sendSMS(array[mMessageSentCount].toString(), message);
}

private void sendNextMessage(){
    if(thereAreSmsToSend()){
        sendSMS(array[mMessageSentCount].toString(), message);
    }else{
        Toast.makeText(getBaseContext(), "All SMS have been sent",
                        Toast.LENGTH_SHORT).show();
    }
}

private boolean thereAreSmsToSend(){
    return mMessageSentCount < array.length;
}

private void sendSMS(final String phoneNumber, String message) {
    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";

    SmsManager sms = SmsManager.getDefault();
    ArrayList<String> parts = sms.divideMessage(message);
    mMessageSentTotalParts = parts.size();

    Log.i("Message Count", "Message Count: " + mMessageSentTotalParts);

    ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    for (int j = 0; j < mMessageSentTotalParts; j++) {
        sentIntents.add(sentPI);
        deliveryIntents.add(deliveredPI);
    }

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

private void registerBroadCastReceivers(){

    registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
            case Activity.RESULT_OK:

                mMessageSentParts++;
                if ( mMessageSentParts == mMessageSentTotalParts ) {
                    mMessageSentCount++;
                    sendNextMessage();
                }

                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;
            }
        }
    }, new IntentFilter(SENT));

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

}


Answer 2:

我来了“一般故障”使用此代码发生同样的错误。 但我用阵列[mMessageSentCount]的ToString()。修剪()。 然后它解决我的问题和它的工作的罚款。 谢谢



文章来源: Sending SMS programmatically to multiple people getting Generic Error