在某些手机中发送短信时出现通用故障

时间:2017-05-02 19:45:21

标签: android mobile sms smsmanager

我正在尝试使用通常可用的代码发送少于160个字符的短信。但是对于一些手机而言,我会失败。我也给了所有短信读/写权限。我尝试了两种方式 - 有和没有国家代码的电话号码,但仍然得到相同的错误。为什么我收到此错误?以下是代码。

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

            if(getResultCode()== Activity.RESULT_OK){
                Toast.makeText(getBaseContext(), "SMS sent",
                        Toast.LENGTH_SHORT).show();
            } else if (getResultCode()== SmsManager.RESULT_ERROR_GENERIC_FAILURE){
                Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();
            } else if (getResultCode() == SmsManager.RESULT_ERROR_NO_SERVICE){
                Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();
            }else if (getResultCode() == SmsManager.RESULT_ERROR_NULL_PDU){
                Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
            }else if (getResultCode() == SmsManager.RESULT_ERROR_RADIO_OFF){
                Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(getBaseContext(), "Unknown error!!",
                        Toast.LENGTH_SHORT).show();
            }


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

    //ArrayList<String> texts = sms.divideMessage(message);
    //sms.sendMultipartTextMessage(phoneNumber, null, texts, null, null);

}

以下是我给出的权限。

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

1 个答案:

答案 0 :(得分:0)

这些手机&#39; API高于或等于6.0。如果是这样,您需要明确/以编程方式请求许可。