Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol 'ACCESS_BACKGROUND_LOCATION'

Tags:

android

Actually I'm trying to make my existing app compatible on Android-10 (Q). Below is how I set compile and target sdk versions in my build.gradle file--

android {
    compileSdkVersion 29
    buildToolsVersion '29.0.0'
    defaultConfig {
        applicationId "com.xyz.myapp"
        minSdkVersion 17
        targetSdkVersion 29
        versionCode 83
        versionName "83.0"
        multiDexEnabled true;
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

and below are the support library dependencies I'm using after migrating my app to Android-X

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.multidex:multidex:2.0.0'

I'm done migrating my project to Android-X, also installed Android9.+ (Q) API-Level 29 from sdk manager and still I'm getting "Cannot resolve symbol 'ACCESS_BACKGROUND_LOCATION'" error. I request you to guide me to resolve this issue. Also please let me know if I can provide more details for the same. Thank you.

like image 630
PKMBSD Avatar asked Sep 20 '19 11:09

PKMBSD


People also ask

What is android permission Get_accounts?

android.permission.GET_ACCOUNTS. Allows access to the list of accounts in the Accounts Service. The app uses this permission in an effort to find the device user's name when the support session is presented to a representative.

What is Access_coarse_location and Access_fine_location?

ACCESS_COARSE_LOCATION. Allows an app to access approximate location. String. ACCESS_FINE_LOCATION. Allows an app to access precise location.

What is the purpose of setting the user permission for Access_fine_location?

If your app needs to access the user's location, you must request permission by adding the relevant Android location permissions to your app. Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION . The permission you choose determines the accuracy of the location returned by the API.


2 Answers

When targeting Android 9 or lower

If your app requests either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION, the system automatically adds ACCESS_BACKGROUND_LOCATION to the request.

Access when device is upgraded to Android 10

If a user grants your app access to device location – either ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION – then upgrades their device from Android 9 to Android 10, the system automatically updates the set of location-based permissions granted to your app. The set of permissions that your app receives after the upgrade depends on its target SDK version and its defined permissions.

Read official guideline Privacy changes in Android 10.

if you target SDK 29+ and want to access locations in the background, you should add below in manifest

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Runtime Permission

private static final int PERMISSION_REQUEST_FINE_LOCATION = 99;
private static final int PERMISSION_REQUEST_BACKGROUND_LOCATION = 100;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) 
    {
            if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_GRANTED) {
                if (this.checkSelfPermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
                        != PackageManager.PERMISSION_GRANTED) {
                    if (this.shouldShowRequestPermissionRationale(android.Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {

                        // Dialog for grant Permission
                    }
                    else {

                       // Dialog for Required permission

                    }

                }
            } 
    }
like image 105
IntelliJ Amiya Avatar answered Oct 12 '22 01:10

IntelliJ Amiya


The error message to which you refer is related to the privacy policies related to permissions on Android. Android 10 introduces the ACCESS_BACKGROUND_LOCATION permission. The API level 29 specifies that access to the location of the device in the background requires this permissions. For this you need to set the targetSdkVersion or compileSdkVersion to 29 or higher. Try to make the permission explicit in the Manifest to get permissions in foreground and background

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
like image 23
Ricardo Sanchez Avatar answered Oct 12 '22 01:10

Ricardo Sanchez