Reputation: 9736
In Android 10, when asking for some permission like Location, the system prompts the user 3 options:
Allow APP to access this device's location?
-Allow all the time
-Allow only while using the app
-Deny
Is there a way to know how exactly has been granted? I use this code to know if location is granted:
boolean hasPermission = (ContextCompat.checkSelfPermission(oContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED);
But I need to know if it has been granted "all the time" or just "while using the app". What I really need is to have permission all the time as my app has a background location service. So I need to know if the permission has been granted to to that, or the possibility to ask for the permission but without the option of allowing it "only when using the app".
requestPermissions(Context, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 123);
Upvotes: 14
Views: 12741
Reputation: 2299
You can use this method, I have tested this method on android level 10 and above :
private void checkLocationPermissionState(Context context){
int fineLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
int coarseLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);
boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
(coarseLocation == PackageManager.PERMISSION_GRANTED);
boolean preciseLocationAllowed = (fineLocation == PackageManager.PERMISSION_GRANTED)
&& (coarseLocation == PackageManager.PERMISSION_GRANTED);
if (preciseLocationAllowed) {
Log.e("PERMISSION","Precise location is enabled in Android 12");
} else {
Log.e("PERMISSION","Precise location is disabled in Android 12");
}
if (isAppLocationPermissionGranted) {
Log.e("PERMISSION","Location is allowed all the time");
} else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
Log.e("PERMISSION","Location is allowed while using the app");
}else{
Log.e("PERMISSION","Location is not allowed.");
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
int bgLocation = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION);
boolean isAppLocationPermissionGranted = (bgLocation == PackageManager.PERMISSION_GRANTED) &&
(coarseLocation == PackageManager.PERMISSION_GRANTED);
if (isAppLocationPermissionGranted) {
Log.e("PERMISSION","Location is allowed all the time");
} else if(coarseLocation == PackageManager.PERMISSION_GRANTED){
Log.e("PERMISSION","Location is allowed while using the app");
}else{
Log.e("PERMISSION","Location is not allowed.");
}
} else {
boolean isAppLocationPermissionGranted = (fineLocation == PackageManager.PERMISSION_GRANTED) &&
(coarseLocation == PackageManager.PERMISSION_GRANTED);
if (isAppLocationPermissionGranted) {
Log.e("PERMISSION","Location permission is granted");
} else {
Log.e("PERMISSION","Location permission is not granted");
}
}
}
Upvotes: 1
Reputation: 1091
As of 18th July 2021 based on @gkpln3's answer it is not returning Boolean value but returning Integer value so added a small boolean converter in Kotlin
val backgroundLocationPermissionApproved: Boolean = ActivityCompat.checkSelfPermission(requireContext(),
Manifest.permission.ACCESS_BACKGROUND_LOCATION) > -1
Upvotes: 0
Reputation: 1499
You can use:
boolean backgroundLocationPermissionApproved =
ActivityCompat.checkSelfPermission(this,
permission.ACCESS_BACKGROUND_LOCATION)
There is no possibility to prompt the user without letting him choose the "only when using the app" option.
Upvotes: 6