When I first launch the app and have SMS permission turned off it request SMS permission but if I restart the app it does not ask again. I want to make it so that it request permission only the first two times the user denies permissions.
// Inside of onCreate()
loadNumberOfDenies();
// Request permission only first two times
if (numberOfDenies < 3) { // stored in saved preferences file
String[] permissions = {Manifest.permission.RECEIVE_SMS};
ActivityCompat.requestPermissions(this, permissions, 1);
}
/**
* Check if the user accepts the permission and if not call permissionDenied
*/
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] results) {
super.onRequestPermissionsResult(requestCode, permissions, results);
if (results.length > 0 && results[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(tag, "User agreed to messaging permission");
} else {
permissionDenied(findViewById(android.R.id.content));
}
}
/**
* Method to shows snack bars if user denies permissions
* @param v the view
*/
private void permissionDenied(View v) {
numberOfDenies++;
saveNumberOfDenies(); // save to shared preferences
Log.d(tag, "Number of denies: " + numberOfDenies);
if (numberOfDenies == 1) {
onShowSnackBar(v, "Without permissions this app cannot receive messages. Restart to configure.");
} else if (numberOfDenies == 2) {
onShowSnackBar(v, "Without permissions this app cannot receive messages. Go to settings to reconfigure Permissions");
}
}
I tried logging the number of denies and it shows that number of denies is equal to two but the app does not request permissions.