请求小组件的位置权限访问。

huangapple go评论59阅读模式
英文:

Request access for location permission in widget

问题

我在清单文件中有ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATION权限,但我假设我需要实时获取权限。在https://developer.android.com/training/location/permissions中有一个示例

val locationPermissionRequest = registerForActivityResult(
        ActivityResultContracts.RequestMultiplePermissions()
    ) { permissions ->
        when {
            permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
                // 获得精准位置权限。
            }
            permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
                // 仅获得大致位置权限。
            } else -> {
                // 没有获得位置权限。
            }
        }
    }

但出现了“Unresolved reference: registerForActivityResult”编译错误。

所以我想我可以使用ActivityCompat.requestPermissions(),就像https://stackoverflow.com/a/40955157/131391中所述,但此方法的第一个参数是一个Activity,而我只有一个小部件而没有活动。

我不确定接下来要尝试什么,以获取小部件的位置权限。

英文:

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.

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

答案1

得分: 1

你的配置活动,或者你的应用程序的其他界面活动,将需要请求权限。

英文:

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

huangapple
  • 本文由 发表于 2023年5月21日 07:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定