Requesting Permissions at Run Time
Posted: Tue Nov 28, 2017 5:23 pm
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app
This is only required for 'dangerous permission'.
Check this link for the list of permissions:
https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous
For storage permission:
See:
https://developer.android.com/training/permissions/requesting.html
This is only required for 'dangerous permission'.
Check this link for the list of permissions:
https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous
For storage permission:
Code: Select all
import android.os.Build;
private boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(context, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
return true;
} else {
ActivityCompat.requestPermissions( mMainActivity, new String[] {
android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.d(TAG,"permission is granted on sdk<23 ");
return true;
}
}
Code: Select all
// do I need and have the permission ?
if (isStoragePermissionGranted()) {
Log.v(TAG,"Storage write allowed");
} else {
Log.w(TAG,"Storage write not allowed");
Toast.makeText(context, "You didn't allow writing to the media", Toast.LENGTH_LONG).show();
return;
}
See:
https://developer.android.com/training/permissions/requesting.html