Reputation: 433
I am trying to build a simple example regarding the permissions request process in Android N.
I have declared the usage of the ACCESS_COARSE_LOCATION
usage inside my manifest file. And I am trying to handle the request inside its own method:
public boolean permissionsChecker() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation 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(MainActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_REQUEST_PERMISSION_COARSE_LOCATION);
return true;
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
return false;
}
This gets asked first when the app connects:
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if(permissionsChecker()){
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
}
if(mLastLocation != null){
Log.i(TAG, mLastLocation.getLatitude() + "");
Log.i(TAG, mLastLocation.getLongitude() + "");
} else {
Log.i(TAG, "Still awaiting permissions, something went wrong");
}
}
My problem is that it was still returning null for the mLastLocation
instance and I was able to see it in the debugger, according to the docs I have to set everything up once there is a request code inside the onRequestPermissionsResult()
callback, but when I try to set the mLastLocation instance inside of it I get the red line telling me that I need to make a permission request:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case MY_REQUEST_PERMISSION_COARSE_LOCATION: {
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
// permissiosn was granted yaaaay! Do the coarse location related task you need to do
//Log.i(TAG, mLastLocation.getLongitude() + "");
//Log.i(TAG, mLastLocation.getLatitude() + "");
//mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
} else {
// permissions denied, boo! Disable the functionality that depends on this permissions
}
return;
// Still inside the case checker, perform other permissions that this app might require working on
} // end of case test for the acces coarse location
} // end of stich statmeent
} // end of onRequestPermissionsResult()
What is the purpose of asking for permissions if the callback is going to make me ask for it again? what am I doing wrong? I am not getting much from the docs(lack of experience I know) and would like to know how to fix this. Any pointers and code suggestions would be greatly appreciated!
Upvotes: 1
Views: 283
Reputation: 15775
The permissionChecker()
usage looks like it is throwing things off. Just because you have requested the permission does not mean it has been granted.
This Droicon talk will help explain the permissions model and usage: https://youtu.be/WGz-alwVh8A
You may also find this library helpful for keeping your code simpler and easier to follow: https://github.com/hiqes/andele
Upvotes: 1