SmsManager sendTextMessage不起作用

时间:2018-03-19 15:29:03

标签: android sms smsmanager

SmsManager sendTextMessage在某些情况下无法正常工作。

我创建了一个Android应用程序,它使用SmsManager发送消息,它在仿真器和真实设备上一直运行良好。然而,最近我的用户收到的一些投诉很少,说我的应用无法在他们的设备上发送短信。我非常确定所有权限都被授予,Crashlytics报告中没有崩溃和错误。

我尝试在Android Emulator上模拟这个问题,我意识到当我将蜂窝网络类型更改为“GSM”并将其多次更改为“Full”时,可能会导致SmsManager sendTextMessage无法正常工作。我还添加了发送和交付的待处理意图但没有一个返回错误代码给我。调用sendTextMessage方法后,它完全没有响应。

如果您对此有任何解决方案,请提供帮助。

备注:

  1. 非常确定授予所有权限(READ_SMS,RECEIVE_SMS,SEND_SMS,READ_PHONE_STATE ...)
  2. 调用sendTextMessage方法后,没有来自待处理意图的崩溃和错误代码。
  3. 消息少于10个字符。
  4. public void sendSMS(String phoneNumber, String message) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
    
        PendingIntent sentPI = PendingIntent.getBroadcast(getContext(), 0, new Intent(
                SENT), 0);
    
        PendingIntent deliveredPI = PendingIntent.getBroadcast(getContext(), 0, new Intent(
                DELIVERED), 0);
    
        BroadcastReceiver sendSMS = new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Broadcast_SMSReceiver.mSMSReceiver.receivedSMS(true);
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getContext(), "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getContext(), "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getContext(), "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getContext(), "Radio off",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_SHORT_CODE_NOT_ALLOWED:
                        Toast.makeText(getContext(), "Premium short codes denied",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_SHORT_CODE_NEVER_ALLOWED:
                        Toast.makeText(getContext(), "Premium short codes denied",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_LIMIT_EXCEEDED:
                        Toast.makeText(getContext(), "Reached the sending queue limit",
                                Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(getContext(), getResultCode(),
                                Toast.LENGTH_SHORT).show();
                }
            }
        };
    
    
        BroadcastReceiver deliverSMS = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getContext(), "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getContext(), "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
    
        getActivity().registerReceiver(sendSMS, new IntentFilter(SENT));
        getActivity().registerReceiver(deliverSMS, new IntentFilter(DELIVERED));
        try {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, "test", sentPI, deliveredPI);
        }catch (Exception e){
            Toast.makeText(getContext(),
                    "SMS failed, please try again later ! ",
                    Toast.LENGTH_LONG).show();
            e.printStackTrace();
    
        }
    
        Broadcast_SMSReceiver.mSMSReceiver.receivedSMS(true);
    }
    

1 个答案:

答案 0 :(得分:0)

我认为您的问题可能出在用户要发送的消息超过160个字符。

看起来,如果您呼叫SmsManager::sendTextMessage(...)的消息比允许的时间长,则呼叫会静默失败。不会引发异常,并且sendPI意图也不会执行。