Question Detail

i am not able to ask for Runtime Permission in android, my Android Application getting Crash after Lolipop Version and above Please suggest me the right way to set Runtime permission.


Thread Reply

Hemant Sharma

- 9 months ago

private static final int REQUEST_READ_STORAGE = 0;
next_Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {

accessComplete();

} });

 

private void accessComplete() {
    if (!mayRequestGallery()) {
        return;
    }
    startActivity(new Intent(FirstActivity.this,GoogleActivity.class));
    finish();
}


private boolean mayRequestGallery() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (checkSelfPermission(READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE)) {
        Snackbar.make(firstScreen_LinearLayout, "Gallery permissions are needed for providing Images\n" +
                "        completions.", Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, REQUEST_READ_STORAGE);
                    }
                });
    } else {
        requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, REQUEST_READ_STORAGE);
    }
    return false;
}

/**
 * Callback received when a permissions request has been completed.
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_STORAGE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults.length == 1) {
            accessComplete();
        }
    }
}

Hemant Sharma

- 9 months ago

private static final int REQUEST_READ_STORAGE = 0;
next_Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {

accessComplete();

} });

 

private void accessComplete() {
    if (!mayRequestGallery()) {
        return;
    }
    startActivity(new Intent(FirstActivity.this,GoogleActivity.class));
    finish();
}


private boolean mayRequestGallery() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (checkSelfPermission(READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE)) {
        Snackbar.make(firstScreen_LinearLayout, "Gallery permissions are needed for providing Images\n" +
                "        completions.", Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, REQUEST_READ_STORAGE);
                    }
                });
    } else {
        requestPermissions(new String[]{READ_EXTERNAL_STORAGE}, REQUEST_READ_STORAGE);
    }
    return false;
}

/**
 * Callback received when a permissions request has been completed.
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_STORAGE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults.length == 1) {
            accessComplete();
        }
    }
}