在android中使用Sms Manager发送短信时RESULT_ERROR_GENERIC_FAILURE返回

时间:2014-01-31 10:41:53

标签: android android-layout android-intent android-fragments

我正在使用Sms Manager向多个用户发送短信,但我收到了RESULT_ERROR_GENERIC_FAILURE(通用失败)。如何解决此问题?

// ---sends an SMS message to another device---
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);

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

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

这里我使用SmsManager和sendSms()取数字和消息作为参数。

3 个答案:

答案 0 :(得分:1)

可能是你应该缩短你的短信身体只适合1个短信(只有160个字符),我使用相同的代码&得到完全相同的错误&当我缩短160以下的短信时,它被成功发送:)

答案 1 :(得分:0)

如果您的邮件正文包含超过160个字符,则需要将SMS作为多部分发送。

答案 2 :(得分:0)

如果SMS太长,您可以将其拆分并作为部分发送:

            val messageToSend = ...
            val smsManager = SmsManager.getDefault()
            val parts = smsManager.divideMessage(messageToSend)
            for (number in smsNumbersToSend)
                smsManager.sendMultipartTextMessage(number, null, parts, null, null)