简体   繁体   English

不发送消息

[英]Not sending message

hi i am using this code to send sms on any number but it is working for the first time and there is no next time.嗨,我正在使用此代码向任何号码发送短信,但它是第一次工作,并且没有下一次。 any fix?任何修复? is there a problem in MY_PERMISSION_REQUEST_SEND_SMS? MY_PERMISSION_REQUEST_SEND_SMS 有问题吗? Is there any other short method to 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;
            }
        }
    }

You don't have the condition when you already have the access to Send SmS permission当您已经拥有发送短信权限时,您没有条件

Put else for outer if and execute your send sms code将 else 放在外部 if 并执行您的发送短信代码

This is because you missed to call smsManager send sms if permission granted这是因为您错过了在授予权限的情况下调用 smsManager 发送短信
Better make a method,最好弄个方法,

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
}

and call this method after permission granted also,并在授予权限后调用此方法,

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

}

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

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