以编程方式将SMS发送给多人获取通用错误

时间:2012-12-28 07:02:36

标签: android generics sms

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

由于SMS很长,我必须使用位于

之下的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个部分,即使延迟3000毫秒,android系统也无法及时发送消息。

    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中,它在服务中执行。

2 个答案:

答案 0 :(得分:20)

IHMO你没有使用正确的方法。我正在使用另一个,它正在工作。我试着解释一下:

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

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

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

}

答案 1 :(得分:2)

我遇到了同样的错误“通用故障”发生时使用此代码。但我使用数组[mMessageSentCount] .toString()。trim()。 然后它解决了我的问题&amp;它的工作正常。 感谢