如何在 Jetpack Compose 中启用位置?
How to enable location in jetpack compose?
我正在请求位置权限,即使在用户授予位置权限后,如果位置设置被禁用,我也无法获得 GPS 位置。我如何请求用户启用 Jetpack Compose 中的位置设置?
val multiplePermissionsState = rememberMultiplePermissionsState(
listOf(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_BACKGROUND_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION
)
)
Button(onClick = {
multiplePermissionsState.launchMultiplePermissionRequest()
},
modifier = Modifier.background(MaterialTheme.colors.primary)
)
创建启用位置权限对话框,如 google 地图。
import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.IntentSender
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.google.android.gms.common.api.ResolvableApiException
import com.google.android.gms.location.*
import com.google.android.gms.tasks.Task
@Composable
fun Layout() {
val context: Context = LocalContext.current
val settingResultRequest = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult()
) { activityResult ->
if (activityResult.resultCode == RESULT_OK)
Log.d("appDebug", "Accepted")
else {
Log.d("appDebug", "Denied")
}
}
Button(onClick = {
checkLocationSetting(
context = context,
onDisabled = { intentSenderRequest ->
settingResultRequest.launch(intentSenderRequest)
},
onEnabled = { /* This will call when setting is already enabled */ }
)
}) {
Text(text = "Request permission")
}
}
// call this function on button click
fun checkLocationSetting(
context: Context,
onDisabled: (IntentSenderRequest) -> Unit,
onEnabled: () -> Unit
) {
val locationRequest = LocationRequest.create().apply {
interval = 1000
fastestInterval = 1000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
val client: SettingsClient = LocationServices.getSettingsClient(context)
val builder: LocationSettingsRequest.Builder = LocationSettingsRequest
.Builder()
.addLocationRequest(locationRequest)
val gpsSettingTask: Task<LocationSettingsResponse> =
client.checkLocationSettings(builder.build())
gpsSettingTask.addOnSuccessListener { onEnabled() }
gpsSettingTask.addOnFailureListener { exception ->
if (exception is ResolvableApiException) {
try {
val intentSenderRequest = IntentSenderRequest
.Builder(exception.resolution)
.build()
onDisabled(intentSenderRequest)
} catch (sendEx: IntentSender.SendIntentException) {
// ignore here
}
}
}
}
您可以使用 Google 的伴奏库:
(https://google.github.io/accompanist/permissions/)
由于他们的网站包含示例代码,因此我不会在此处粘贴代码。
我正在请求位置权限,即使在用户授予位置权限后,如果位置设置被禁用,我也无法获得 GPS 位置。我如何请求用户启用 Jetpack Compose 中的位置设置?
val multiplePermissionsState = rememberMultiplePermissionsState(
listOf(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_BACKGROUND_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION
)
)
Button(onClick = {
multiplePermissionsState.launchMultiplePermissionRequest()
},
modifier = Modifier.background(MaterialTheme.colors.primary)
)
创建启用位置权限对话框,如 google 地图。
import android.app.Activity.RESULT_OK
import android.content.Context
import android.content.IntentSender
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.IntentSenderRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.google.android.gms.common.api.ResolvableApiException
import com.google.android.gms.location.*
import com.google.android.gms.tasks.Task
@Composable
fun Layout() {
val context: Context = LocalContext.current
val settingResultRequest = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult()
) { activityResult ->
if (activityResult.resultCode == RESULT_OK)
Log.d("appDebug", "Accepted")
else {
Log.d("appDebug", "Denied")
}
}
Button(onClick = {
checkLocationSetting(
context = context,
onDisabled = { intentSenderRequest ->
settingResultRequest.launch(intentSenderRequest)
},
onEnabled = { /* This will call when setting is already enabled */ }
)
}) {
Text(text = "Request permission")
}
}
// call this function on button click
fun checkLocationSetting(
context: Context,
onDisabled: (IntentSenderRequest) -> Unit,
onEnabled: () -> Unit
) {
val locationRequest = LocationRequest.create().apply {
interval = 1000
fastestInterval = 1000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
val client: SettingsClient = LocationServices.getSettingsClient(context)
val builder: LocationSettingsRequest.Builder = LocationSettingsRequest
.Builder()
.addLocationRequest(locationRequest)
val gpsSettingTask: Task<LocationSettingsResponse> =
client.checkLocationSettings(builder.build())
gpsSettingTask.addOnSuccessListener { onEnabled() }
gpsSettingTask.addOnFailureListener { exception ->
if (exception is ResolvableApiException) {
try {
val intentSenderRequest = IntentSenderRequest
.Builder(exception.resolution)
.build()
onDisabled(intentSenderRequest)
} catch (sendEx: IntentSender.SendIntentException) {
// ignore here
}
}
}
}
您可以使用 Google 的伴奏库: (https://google.github.io/accompanist/permissions/)
由于他们的网站包含示例代码,因此我不会在此处粘贴代码。