简体   繁体   English

SmsManager sendTextMessage不起作用

[英]SmsManager sendTextMessage Not Working

SmsManager sendTextMessage not working in some conditions. SmsManager sendTextMessage在某些情况下不起作用。

I created an android application which uses SmsManager to send message and it was working fine all the time on emulator and real devices. 我创建了一个使用SmsManager发送消息的android应用程序,它在模拟器和真实设备上一直运行良好。 However recently I received few complaints from my user saying that my app could not send SMS on their devices. 但是最近,我收到用户的抱怨很少,称我的应用无法在他们的设备上发送短信。 I pretty sure all permissions are granted and no crash and error in Crashlytics report. 我很确定在Crashlytics报告中授予了所有权限,并且没有崩溃和错误。

I tried simulate this issue on Android Emulator and I realized that when i change cellular network type to "GSM" and change it back to "Full" several times it might cause the SmsManager sendTextMessage not working. 我尝试在Android Emulator上模拟此问题,并且意识到当我将蜂窝网络类型更改为“ GSM”并将其更改回“ Full”多次时,可能会导致SmsManager sendTextMessage无法正常工作。 I also added sent and delivered pending intents but none of it return error code to me. 我还添加了已发送和已交付的未决意图,但是没有一个会向我返回错误代码。 It totally no response after calling sendTextMessage method. 调用sendTextMessage方法后,它完全没有响应。

Please help if you have any solution about this. 如果您对此有任何解决方案,请提供帮助。

Notes: 笔记:

  1. Very sure all permission are granted (READ_SMS, RECEIVE_SMS, SEND_SMS, READ_PHONE_STATE...) 非常确保所有权限都被授予(READ_SMS,RECEIVE_SMS,SEND_SMS,READ_PHONE_STATE ...)
  2. No crash and error code from pending intent after calling sendTextMessage method. 调用sendTextMessage方法后,暂挂意图没有崩溃和错误代码。
  3. Message is less than 10 characters. 讯息少于10个字元。

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

I think your problem could be with the message that the user wanted to send was more than 160 characters. 我认为您的问题可能出在用户要发送的消息超过160个字符。

It looks like, that if you call SmsManager::sendTextMessage(...) with a message that is longer than allowed, the call silently fails. 看起来,如果您使用比允许的时间更长的消息来调用SmsManager::sendTextMessage(...) ,则调用将以静默方式失败。 No Exception is thrown, and the sentPI intent is not executed either. 没有抛出异常,并且sendPI意图也没有执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM