How to Handle storage permissions in Marshmallow.

Explain why the app needs permissions

In some circumstances, we recommend helping the user understand why your app needs permission. For example, if a user initiates a photo app, they probably will not be surprised if the app asks for permission to use the camera. However, the user may not understand why the app seeks to access the user's location or contacts. Before requesting a permit, you should consider providing an explanation to the user. Remember that you will not want to overwhelm the user with explanations; if you do, the user may feel frustrated and delete the app.

One approach you could use is to provide an explanation only if the user rejects the permission request. If the user continues to try to use a functionality that requires a permission, but continues to reject the permission request, this probably tells us not to understand why the app needs permission to provide that functionality. In a situation like this, you probably want to show an explanation.

To help detect situations in which the user might need an explanation, Android provides a utility method: shouldShowRequestPermissionRationale(). This method shows trueif the app requests the permission previously and the user rejects the request.


Runtime Permission


Android-M ie, API 23 introduced Runtime Permissions for reducing security flaws in android device, where users can now directly manage app permissions at runtime.so if the user denies a particular permission of your application you have to obtain it by asking the permission dialog that you mentioned in your query.
So check before action ie, check you have permission to access the resource link and if your application doesn't have that particular permission you can request the permission link and handle the the permissions request response like below.
@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG, "Permission: " + permissions[0] + "was " + grantResults[0]);
        //resume tasks needing this permission
    }
}

public boolean isStoragePermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.v(TAG, "Permission is granted");
            // permission was granted, related task you need to do.
            return true;
        } else {
            Log.v(TAG, "Permission is revoked");
            // permission denied, boo! Disable the
            // functionality that depends on this permission.
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
            return false;
        }
    } else { 
        //permission is automatically granted on sdk<23 upon installation        
        Log.v(TAG, "Permission is granted");
        return true;
    }
}

No comments:

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...