I am currently following the tutorial on how to get permission in Android. Now, in the Android tutorial, it says to type the following:
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
GPSExplanation();
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
However, I do not get what MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is supposed to do. It says it should be an app defined int constant. I have put in placebo number 1 to pass by, but I am curious what it actually functions as, and why it must be included
Can someone give me a reason, or example showing the constant in effect?
Thank you in advance.
If you follow the tutorial a bit later:
When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's onRequestPermissionsResult() method, passing it the user response. Your app has to override that method to find out whether the permission was granted. The callback is passed the same request code you passed to requestPermissions(). For example, if an app requests READ_CONTACTS access it might have the following callback method:
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted, yay! Do the // contacts-related task you need to do. } else { // permission denied, boo! Disable the // functionality that depends on this permission. } return; } // other 'case' lines to check for other // permissions this app might request } }
So for example you have requested for MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION
& MY_PERMISSIONS_REQUEST_READ_CONTACTS
in your app, using this integer constant you can take actions accordingly for individual permissions.