英文:
Request access for location permission in widget
问题
我在清单文件中有ACCESS_FINE_LOCATION
和ACCESS_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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论