{
  "$type": "site.standard.document",
  "canonicalUrl": "https://segunfamisa.com/posts/marshmallow-permissions",
  "description": "Quick update on using the new runtime permissions on Android 6.0 (Marshmallow)",
  "path": "/posts/marshmallow-permissions",
  "publishedAt": "2016-01-28T02:05:30.000Z",
  "site": "at://did:plc:a5mekodp4afxadlpr4hp2wci/site.standard.publication/3mm2oa7vz5327",
  "tags": [
    "android",
    "tips"
  ],
  "textContent": "<p align=\"center\">\n\t<img src=\"/images/android–marshmallow.png\">\n</p>\n\nAfter a long wait, my AndroidOne finally got it’s Marshmallow update yesterday.\nI finally got a hands on experience of the not-so-new OS, and I must confess,\nI love it! I love the ‘Now on tap’, the ‘DND’ mode, and one of my favourite parts\nof the update - the new permissions feature.\n\nWith the new permissions, users now have more control over permissions that apps have.\nLike in iOS, users can specifically turn off certain permissions like access to contacts,\nlocations etc. So, if you’ve built your app previously to request for permission\nto use the camera for example, if users turn off that permission for your app,\nthat functionality will fail. Check here\n if you want to see more about the ‘new’ permissions in Android M.\n\nThis has been out for a while now and, but I thought to put out some tips here for\n developers yet to update their apps for Android Marshmallow.\n\n According to Nick Butcher in this\n video, the primary purpose of permission, is to protect your users' privacy.\n Runtime permissions allow you to choose the right time to ask for permission when\n  the user has more context about why you’re asking and why they need to grant it.\nHe also described the UX best practices with runtime permissions. I suggest that\n you check out the video. He talked about determining your permission requirements by\n how important (critical or not) the permission is to your app, and how clear the\nneed for that permission is.\n\n- Critical and clear: e.g SMS permission in an SMS app, or Camera permission in a Camera app.\nThis is critical to app functionality and hence, it's good to request for this permission at the beginning.\n\n- Critical and unclear: when the feature is important, but not immediately obvious.\nYou should educate the users upfront before requesting for permission. Secondary and\nnot clear: offer some explanation and then allow the users opt-in and then should you request.\n\n- Secondary and clear: If a secondary feature that is clear, you should ask in context.\nNo need to ask too early. Better to wait till when they try to use the feature,\nwhen the purpose becomes clear.\n\n- Secondary and not clear: For a secondary feature that is not really obvious,\nask for permission only when the user attempts to use that feature.\n\nPlease allow me.\nSo, let's say we're making a new really cool app and we want to - say scan a QR code.\n\nWe obviously need the android:name=\"android.permission.CAMERA\" permission, so\nwe add it to our AndroidManifest.xml as usual.\n\nSo, before we launch the camera, we do a check like:\n    {% highlight java %}\n    if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)\n            != PackageManager.PERMISSION_GRANTED) {\n        //if permission is not granted.\n        if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {\n            //this is an utility method to handle case where the user has previously declined.\n            //you may need to show an explanation.\n            Snackbar.make(mCameraPreview, \"You need to enable this app to access your camera\",\n                    Snackbar.LENGTH_INDEFINITE).setAction(\"Settings\", new View.OnClickListener() {\n                @Override\n                public void onClick(View v) {\n                    //open settings\n                    showInstalledAppDetails(getApplicationContext(), getPackageName());\n                }\n            }).show();\n        } else {\n            //request for permission. You can listen for a response via onRequestPermissionsResult method.\n            //note that REQUEST_PERMISSION_CAMERA is just an int request code taht we'll use to listen for the response\n            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_PERMISSION_CAMERA);\n        }\n    } else {\n        //if permission is granted.\n        //do stuff with the camera\n    }\n    {% endhighlight %}\n\nNote that to use this from a fragment, you need to call shouldShowRequestPermissionRationale and requestPermissions\nstraight form the fragment.\n\nNow that we've asked\nNow that we've asked for permission, we should be able to find out whether the\nuser has granted permission or not. Thank goodness, Android already has a callback method onRequestPermissionsResult)\n\n{% highlight java %}\n  @Override\n  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {\n      if(requestCode == REQUEST_PERMISSION_CAMERA) {\n          //for the camera permission request\n          //check if the grantResults array is not empty and it contains PackageManager.PERMISSION_GRANTED\n          if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){\n              //openCamera\n              openAndAttachCamera();\n          } else{\n              //permission has been denied. Disable the feature.\n          }\n      } else {\n          super.onRequestPermissionsResult(requestCode, permissions, grantResults);\n      }\n  }\n{% endhighlight %}\n\nAnddd that's it.\nNow, you know how to work with runtime permissions.\nThings change very fast with Android, always be sure to check out https://developer.android.com and https://android-developers.blogspot.com for latest info.\n\nIn case you need a full app demo for this, check the source here: https://github.com/segunfamisa/MarshmallowPermissions.\n\nPlease share and drop any comments in the comments section below.\nThanks for reading :)",
  "title": "May I have some Marshmallows, please?"
}