发送短信失败

时间:2018-04-25 10:42:41

标签: android

我制作了一个小型Android应用程序,只需发送短信。

我在清单中添加了Send_SMS权限。

我的代码如下:

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS},1);

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                sendSMS(phoneNumberTo, smsMessage);
            }
            return;
        }
    }
}

private void sendSMS(String phoneNumber, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

我的手机正在尝试发送短信,但收到错误:

enter image description here

我应该补充一点:

  1. 当我点击手机时,#34;重新发送"它的工作原理
  2. 我正在使用xiomi note 4

2 个答案:

答案 0 :(得分:1)

试试这个

 SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message); 
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);

答案 1 :(得分:0)

我认为问题在于在运行时获得权限。此代码可以帮助您

if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.SEND_SMS)
    != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.SEND_SMS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.SEND_SMS},
                MY_PERMISSIONS_REQUEST_SEND_SMS);
    }
} else {
    // message sending code here
}