简体   繁体   中英

My app is not requesting for location permission and the permission is denied automatically

I have requested location permission in my manifest file as follows -

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

I also requested run time permission as follows -

    //get location permission
    int locationPermissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);

    if (locationPermissionCheck != PackageManager.PERMISSION_GRANTED) {
        Toast.makeText(mContext, "Location permission denied", Toast.LENGTH_SHORT).show();
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_FINE_LOCATION);
    } else {
        Toast.makeText(mContext, "Location permission granted", Toast.LENGTH_SHORT).show();
    }

When I install the app from android studio to a connected device(android 10) then I see no request for location, instead I see the toast message "Location permission denied". Then I go to app permission and see the location permission is denied. Then I allow it, close the app and reopen it and then it works. Then I uninstall the app and install again. And then the location permission is denied again automatically even before I run the app.

I am requesting run time permission from MainActivty and using Fused Location in a foreground service. I tried

((ActivityManager)context.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();

with no luck.

On Android 10 (API level 29) and higher, you must declare the ACCESS_BACKGROUND_LOCATION permission in your app's manifest in order to request background location access at runtime. On earlier versions of Android, when your app receives foreground location access, it automatically receives background location access as well.

<manifest ... >
  <!-- Required only when requesting background location access on
       Android 10 (API level 29) and higher. -->
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
</manifest>

Visit Android Location Documentation for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM