13

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);
Community
  • 1
  • 1
Ton
  • 9,235
  • 15
  • 59
  • 103

3 Answers3

5

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.

gkpln3
  • 1,317
  • 10
  • 24
  • 1
    Also see [this](https://stackoverflow.com/a/58823523/6759241); you can't get rid of the "Allow only while using the app" option, but you can get rid of the other one. – greeble31 Dec 15 '19 at 13:36
  • thats not true this result return to Allow All the Time and Allow only while using the app so you cant check only all the time this way. – Umut ADALI Jun 29 '20 at 08:33
1

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");
    }
}

}

Himanshi Thakur
  • 2,077
  • 14
  • 32
0

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
Sayok Majumder
  • 1,012
  • 13
  • 28