Reputation: 167
I have an app to get user location with permissions.My target API=25 btw
Once I have given permission that It doesn't ask me for request permission anymore.
I have checked with debug my hasCoarseLocationPermission
and hasFineLocationPermission
always return 0(GRANTED) after giving permission.
But if I clear the app data asking for permission again(returns -1).But I always want to ask for permission when I open the application(must return -1 not 0).How can i do that?
Thanks in advance.
int hasCoarseLocationPermission=ContextCompat.checkSelfPermission(Kirala3.this, Manifest.permission.ACCESS_COARSE_LOCATION);
int hasFineLocationPermission = ContextCompat.checkSelfPermission(Kirala3.this, Manifest.permission.ACCESS_FINE_LOCATION);
I've tried also with PermissionChecker function but it returns same value(0).
Upvotes: 0
Views: 2334
Reputation: 15775
Asking the user for permissions is not something an app is supposed to do at each launch. Once the app has been granted a permission, it has the permission until it is revoked by the user. What you describe is happening sounds correct:
checkSelfPermission
and gets back PERMISSION_DENIED
checkSelfPermission
(which is exactly what it should do.) Now the returned value is PERMISSION_GRANTED
. This is expected. No new permission request from the user is required.checkSelfPermission
returns PERMISSION_DENIED
, the app must ask the user for permission again.You may find this talk helpful: https://youtu.be/WGz-alwVh8A
You may also find this permissions helper library useful: https://github.com/hiqes/andele
Upvotes: 0
Reputation: 4510
Use this function for checking location permission
public boolean isLocationPermissionEnabled() {
return !(Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED);
}
Upvotes: 0
Reputation: 1221
Try with ActivityCompat
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Upvotes: 1