Skip to content Skip to sidebar Skip to footer

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? Is

Solution 1:

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

Solution 2:

This is because you missed to call smsManager send sms if permission granted Better make a method,

privatevoidsmsSend(){
 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,
                newString[]{Manifest.permission.SEND_SMS},
                MY_PERMISSIONS_REQUEST_SEND_SMS);

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

and call this method after permission granted also,

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

}

Post a Comment for "Not Sending Message"