I want to check SMS reading permission is Granted or not in API 23+. So I implemented its as follows;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}
Handling permission resp. as follows;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_SMS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Doing task regarding SMS permission.
}else if (grantResults[0] == PackageManager.PERMISSION_DENIED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_SMS)) {
//Show an explanation to the user *asynchronously*
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This permission is important to Read SMS.")
.setTitle("Important permission required");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ActivityCompat.requestPermissions(GenerateOTPActivity.this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}
});
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS}, PERMISSIONS_REQUEST_READ_SMS);
}else{
//Never ask again and handle your app without permission.
}
}
}
}
By default SMS permission is Granted so checkSelfPermission() reterns zero but when I manually deny permission from device setting then also checkSelfPermission() returns zero value.
I dont understand how to check SMS permission is denied or not. Please provide me some solutions.