0

I want to send SMS by android-app, but the permission dialog show before the send a message every time, I supposed to send a message without dialog show after the first permission asked.

My android app is run on mi-8, target SDK-version is 29.

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, 3);
} else if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 4);
} else {
    sendMsg();
}

I expect the permission dialog did not show when I send a message after the first asked permission, but actual every time show dialog when I send a message .

denvercoder9
  • 2,979
  • 3
  • 28
  • 41
allenMeng
  • 3
  • 1

1 Answers1

0

use this function to check whether you have given the permission if say so you have given as required persmission it will return true flag

check by this way

   private boolean checkAndRequestPermissions(){

        //add as much as permission you need
        int readSMS = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);
        int sendSms = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS);

        List<String> listPermissionsNeeded = new ArrayList<>();

        if (readSMS != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.READ_SMS);
        }
        if (receiveSms != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS);
        }
        if (!listPermissionsNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                    REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }

check by this way

 if (checkAndRequestPermissions()){
        sendMsg();
    }else{
      //do what you want  
    }

no need to do this by hardcode way

Black mamba
  • 462
  • 4
  • 15
  • first, thank your reply, I know how to reformat my code, but the problem is not this, the question is why I granted the permission after sending a message the dialog show again? – allenMeng Sep 19 '19 at 08:31
  • please mention full code if you have manually asked it then it shows the dialog for sure even if you have gave @allenMeng – Black mamba Sep 19 '19 at 09:44
  • ok,thanks, my question will appear depending on android-system. – allenMeng Sep 20 '19 at 09:16