I\'m attempting to request ACCESS_FINE_LOCATION
permissions in order to get the user\'s current location.
My logging indicates that my app does not curr
I will share the code that works for me. In the protected void onCreate(Bundle savedInstanceState) {} method of my activity where I want to see the prompt, I included this code:
/* Check whether the app has the ACCESS_FINE_LOCATION permission and whether the app op that corresponds to
* this permission is allowed. The return value is an int: The permission check result which is either
* PERMISSION_GRANTED or PERMISSION_DENIED or PERMISSION_DENIED_APP_OP.
* Source: https://developer.android.com/reference/android/support/v4/content/PermissionChecker.html
* While testing, the return value is -1 when the "Your location" permission for the App is OFF, and 1 when it is ON.
*/
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
// The "Your location" permission for the App is OFF.
if (permissionCheck == -1){
/* This message will appear: "Allow [Name of my App] to access this device's location?"
* "[Name of my Activity]._instance" is the activity.
*/
ActivityCompat.requestPermissions([Name of my Activity]._instance, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ACCESS_FINE_LOCATION);
}else{
// The "Your location" permission for the App is ON.
if (permissionCheck == 0){
}
}
Before the protected void onCreate(Bundle savedInstanceState) {} method, I created the following constant and method:
public static final int REQUEST_CODE_ACCESS_FINE_LOCATION = 1; // For implementation of permission requests for Android 6.0 with API Level 23.
// Code from "Handle the permissions request response" at https://developer.android.com/training/permissions/requesting.html.
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-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
}
}