不发送消息

时间:2017-02-10 10:08:58

标签: java android

嗨,我正在使用此代码发送任何号码的短信,但它是第一次工作,没有下一次。任何修复? MY_PERMISSION_REQUEST_SEND_SMS有问题吗?有没有其他简短的方法发送短信?

protected void SendSms() {
    phonenumber=number.getText().toString();
    message= txt.getText().toString();
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.SEND_SMS)) {
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.SEND_SMS},
                    MY_PERMISSIONS_REQUEST_SEND_SMS);

        }
    }

}
@Override
public void onRequestPermissionsResult(int requestCode,String permissions[],
    int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_SEND_SMS: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED)    
        {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phonenumber, null, message, null, 
       null);
                Toast.makeText(getApplicationContext(), "SMS sent.",
                        Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again.",  
      Toast.LENGTH_LONG).show();
                return;
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

当您已拥有发送SmS权限

的权限时,您没有这个条件

将else用于外部if并执行发送短信代码

答案 1 :(得分:0)

这是因为如果获得许可,您错过了调用smsManager发送短信的权利 更好地制作方法,

private void smsSend(){
 SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phonenumber, null, message, null, 
   null);
            Toast.makeText(getApplicationContext(), "SMS sent.",
                    Toast.LENGTH_LONG).show();
}

if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.SEND_SMS)
        != PackageManager.PERMISSION_GRANTED) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this,
            Manifest.permission.SEND_SMS)) {
    } else {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.SEND_SMS},
                MY_PERMISSIONS_REQUEST_SEND_SMS);

    }
}
else{
    smsSend(); //call method if already granted
}

并在授予权限后调用此方法,

@Override
    public void onRequestPermissionsResult(int requestCode,String permissions[],
    int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_SEND_SMS: {
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED)    
            {
                smsSend(); //call method after permission grant
            } else {
                Toast.makeText(getApplicationContext(),
                    "SMS faild, please try again.",  
  Toast.LENGTH_LONG).show();
                return;
            }
        }
    }

}