Android Developer Documentation gives this example of requesting permissions at runtime:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
What is "MY_PERMISSIONS_REQUEST_READ_CONTACTS" in this example? It says it's an app-defined int constant, but does that mean I should make a Constants.java and declare a public static int? What should the value be?
In other examples I see people use 1 here, or 0 or 0xFFEEDDCC, but I can't find an explanation of what it is. Can someone explain to me what needs to go here and why? (In my case, I need to make sure the app has permission to access fine location)
The ActivityCompat documentation says "Application specific request code to match with a result reported to onRequestPermissionsResult"? This does not help me.
It is an
int
, to tie a particularrequestPermissions()
call to the correspondingonRequestPermissionsResult()
callback.Under the covers,
requestPermissions()
usesstartActivityForResult()
; thisint
serves the same role as it does instartActivityForResult()
.I would just make it a
private static final int
in the activity. But, you can declare it wherever you want.I seem to recall that it needs to be below 0x8000000, but otherwise it can be whatever you want. The value that you use for each
requestPermissions()
call in an activity should get a distinctint
, but the actual numbers do not matter.If your activity has only one
requestPermissions()
call, then theint
value really does not matter. But many apps will have severalrequestPermissions()
calls in an activity. In that case, the developer may need to know, inonRequestPermissionsResult()
, what request this is the result for.}
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 :)
Look just a little further down in the documentation under "Handle the permissions request response" and you will see its purpose.
A callback method called
onRequestPermissionsResult
gets sent back the same code as a parameter so you know which permission was being requested/granted:Since the constant is used by you only you can give it whatever value you like as a
public static final int
. Each permission being requested needs its own constant.