I want to know the best practise to ask user for permission check and the code to run if user declines that particular permission access.
This example I copied from http://www.applicoinc.com/blog/android-m-permissions-review/ sets state for the CONTACTS
permission:
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 1; private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS} if (checkSelfPermission(PERMISSIONS_CONTACT)) { Log.i(TAG, "Contact permissions have already been granted. Displaying contact details."); } else { Log.i(TAG, "Contact permissions has NOT been granted. Requesting permission."); requestPermissions(PERMISSIONS_CONTACT, PERMISSIONS_REQUEST_READ_CONTACTS); } … @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case PERMISSIONS_REQUEST_READ_CONTACTS: { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! do the // calendar task you need to do. Log.d(TAG, "permission granted"); } else { // permission denied, boo! Disable the // functionality that depends on this permission. Log.d(TAG, "permission denied"); } return; } } }