Android Run time Permissions in Android 6.0 (API level 23) best practices
I have successfully added Permissions on the run time to my app using
These refrences
http://developer.android.com/training/permissions/requesting.html
and
https://www.learn2crack.com/2015/10/android-marshmallow-permissions.html
this is the code I have used
private static final int PERMISSION_REQUEST_CODE = 1;
switch (id){
case R.id.check_permission:
if (checkPermission()) {
Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view,"Please request permission.",Snackbar.LENGTH_LONG).show();
}
break;
case R.id.request_permission:
if (!checkPermission()) {
requestPermission();
} else {
Snackbar.make(view,"Permission already granted.",Snackbar.LENGTH_LONG).show();
}
break;
}
private boolean checkPermission(){
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
if (result == PackageManager.PERMISSION_GRANTED){
return true;
} else {
return false;
}
}
private void requestPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.ACCESS_FINE_LOCATION)){
Toast.makeText(context,"GPS permission allows us to access location data. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Snackbar.make(view,"Permission Granted, Now you can access location data.",Snackbar.LENGTH_LONG).show();
} else {
Snackbar.make(view,"Permission Denied, You cannot access location data.",Snackbar.LENGTH_LONG).show();
}
break;
}
}
The best practices provided by android
http://developer.android.com/training/permissions/best-practices.html
state the following
1) Consider Using an Intent
2) Only Ask for Permissions You Need
3) Don't Overwhelm the User
4) Explain Why You Need Permissions
now following the above we ask permissions only when needed and only for the specific permission which is required by that activity
However These best practices donot state the frequency of the permission , ie to say how many times these permission need to be shown or can it be shown only once
now the problem that I am facing is
1) each time the activity is called in the app the permissions pop up, These happens for that particular session of app usage(if same activity is called during the same session the permission is not called for once it is granted)
2) Can we store permissions that have been granted (ie the static variable which performs the first check in shared preference so that permissions are not called again and again)
3) Do we need to call for the permission access each time the app is used
4) If shared preferences are not allowed how to we manage these permissions more elegantly