I am currently working on an application that requires several "dangerous" permissions. So I tried adding "ask for permission" as required in Android Marshmallow(API Level 23), but couldn't find how to do it.
How can I ask for permission using new permission model in my app?
It may be a cleaner way. Add all your permissions in an array like
Whatever your permision is create method for each permision
Call this method in onCreate
Now override onRequestPermissionsResult
I went through all answers, but doesn't satisfied my exact needed answer, so here is an example that I wrote and perfectly works, even user clicks the Don't ask again checkbox.
Create a method that will be called when you want to ask for runtime permission like
readContacts()
or you can also haveopenCamera()
as shown below:Now we need to make
askContactsPermission()
, you can also name it asaskCameraPermission()
or whatever permission you are going to ask.Before writing this function make sure you have defined the below instance variable as shown:
Now final step to override the
onRequestPermissionsResult
method as shown below:Here we are done with the RunTime permissions, the addon is the
openPermissionSettingDialog()
which simply open the Setting screen if user have permanently disable the permission by clicking Don't ask again checkbox. below is the method:What we missed ? 1. Defining the used strings in
strings.xml
Initializing the
parentLayout
variable insideonCreate
methodparentLayout = findViewById(R.id.content);
Defining the required permission in
AndroidManifest.xml
The
queryContacts
method, based on your need or the runtime permission you can call your method before which thepermission
was needed. in my case I simply use the loader to fetch the contact as shown below:This works great happy coding :)