Hello I am deleting some items from SD card. And in lollipop first time i ask for permission and user grant permission, but now i don't want to ask every time for permission. Its very irritating and many of guys they afraid from this behavior. How can i save my permission i.e. next time i don't need to pass that Intent again and again
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
Thank you
I give you some code to example. Remember that u need check permission on Android 6.0. OnRequestPermission is method that when u have permission will be do code in grantResult, if not u can ask for permission or saying message that user not give a permission. And this method save status for permission
@TargetApi(23)
public void checkStoragePermission() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (settingsDrawerFragment != null) {
settingsDrawerFragment.onPermissionGranted();
}
return;
}
if (this.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager
.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_CODE_EXTERNAL_STORAGE);
return;
}
if (settingsDrawerFragment != null) {
settingsDrawerFragment.onPermissionGranted();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]
grantResults) {
switch (requestCode) {
case REQUEST_CODE_AUDIO_RECORD:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startBrowseActivity();
} else {
Utils.showToast(this, getString(R.string.audio_permission_denied));
}
break;
case REQUEST_CODE_EXTERNAL_STORAGE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (settingsDrawerFragment != null) {
settingsDrawerFragment.onPermissionGranted();
}
} else {
if (settingsDrawerFragment != null) {
closeSettingsDrawer();
}
Utils.showToast(this, getString(R.string.storage_permission_denied));
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
You can use the following code to persist permission
getContentResolver().takePersistableUriPermission(treeUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Also once you persist permission save the uri in a sharedPreference so that you can delete files later without confirmation.
You can launch your intent by adding falgs.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent);