Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if location permission is granted "Allow all the time"

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);
like image 751
Ton Avatar asked Dec 15 '19 11:12

Ton


People also ask

Does Google Maps need location permission all the time?

Open Settings > Location > App Permission > Maps. Then choose between the available options to use location all the time or only while using the app. For the full features of Google Maps to work it's best to allow location access all the time.

How do you check all permission is granted in Android?

To check if the user has already granted your app a particular permission, pass that permission into the ContextCompat. checkSelfPermission() method. This method returns either PERMISSION_GRANTED or PERMISSION_DENIED , depending on whether your app has the permission.

How do I give runtime permission to location in Android?

This example demonstrates how do I request Location permission in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.


1 Answers

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.

like image 58
gkpln3 Avatar answered Oct 10 '22 02:10

gkpln3