Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Request access for location permission in widget

I have both ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION in my manifest but assume I need to get the permission granted in realtime. On https://developer.android.com/training/location/permissions there is a sample

val locationPermissionRequest = registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) { permissions ->
        when {
            permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
                // Precise location access granted.
            }
            permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
                // Only approximate location access granted.
            } else -> {
                // No location access granted.
            }
        }
    }

but it has compilation "Unresolved reference: registerForActivityResult"

So I thought I could use ActivityCompat.requestPermissions() as in https://stackoverflow.com/a/40955157/131391 but the first parameter for this method is an Activity, while I have only a widget and no activity.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m not sure what to try next, to get location permission for my widget.

>Solution :

Your configuration activity, or some other activity in your app’s UI, will need to request the permissions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading