gpt4 book ai didi

android - FusedLocationProviderClient 与 Kotlin 协程

转载 作者:行者123 更新时间:2023-12-02 12:07:12 28 4
gpt4 key购买 nike

我正在尝试使用 FusedLocationProviderClient 和 Kotlin Coroutines 请求一个新位置。这是我目前的设置:

class LocationProviderImpl(context: Context) : LocationProvider, CoroutineScope {

private val TAG = this::class.java.simpleName

private val job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.IO

private val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
private val locationRequest = LocationRequest().apply {
numUpdates = 1
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}

override suspend fun getLocation(): LatLng = suspendCoroutine {
val locationCallback = object : LocationCallback() {
override fun onLocationResult(result: LocationResult) {
result.lastLocation.run {
val latLng = latitude at longitude
it.resume(latLng)
}

fusedLocationProviderClient.removeLocationUpdates(this)
}
}

try {
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
} catch (e: SecurityException) {
throw NoLocationPermissionException()
}
}
}

但是在尝试请求新位置时,出现以下异常:
java.lang.IllegalStateException: Can't create handler inside thread that has not called Looper.prepare()

但是,如果我调用 Looper.prepare() (最终调用 Looper.quit() )是否意味着我只能调用该函数一次?

任何帮助表示赞赏。

最佳答案

private val locationRequestGPS by lazy {
LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setNumUpdates(1)
.setExpirationDuration(1000)
}

private val locationRequestNETWORK by lazy {
LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_LOW_POWER)
.setNumUpdates(1)
.setExpirationDuration(1000)
}



suspend fun getLocation(context: Context, offsetMinutes: Int = 15): Location? = suspendCoroutine { task ->
val ctx = context.applicationContext
if (!ctx.isPermissionValid(Manifest.permission.ACCESS_COARSE_LOCATION)
&& !ctx.isPermissionValid(Manifest.permission.ACCESS_FINE_LOCATION)) {
task.resume(null)
} else {
val manager = ctx.getSystemService(Context.LOCATION_SERVICE) as LocationManager
if (!LocationManagerCompat.isLocationEnabled(manager)) {
task.resume(null)
} else {
val service = LocationServices.getFusedLocationProviderClient(ctx)
service.lastLocation
.addOnCompleteListener { locTask ->
if (locTask.result == null || System.currentTimeMillis() - locTask.result!!.time > offsetMinutes.minute) {
GlobalScope.launch(Dispatchers.Main) {
task.resume(locationRequest(manager, service))
}
} else {
task.resume(locTask.result)
}

}
}
}
}

suspend fun getLocationLast(context: Context): Location? = suspendCoroutine { task ->
val ctx = context.applicationContext
if (!ctx.isPermissionValid(Manifest.permission.ACCESS_COARSE_LOCATION)
&& !ctx.isPermissionValid(Manifest.permission.ACCESS_FINE_LOCATION)) {
task.resume(null)
} else {
if (!LocationManagerCompat.isLocationEnabled(ctx.getSystemService(Context.LOCATION_SERVICE) as LocationManager)) {
task.resume(null)
} else {
LocationServices.getFusedLocationProviderClient(ctx)
.lastLocation
.addOnCompleteListener { locTask ->
task.resume(locTask.result)
}
}

}
}

suspend fun locationRequest(locationManager: LocationManager, service: FusedLocationProviderClient): Location? = suspendCoroutine { task ->
val callback = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult?) {
service.removeLocationUpdates(this)
task.resume(p0?.lastLocation)
}
}

when {
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) -> {
service.requestLocationUpdates(locationRequestGPS, callback, Looper.getMainLooper())
}
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) -> {
service.requestLocationUpdates(locationRequestNETWORK, callback, Looper.getMainLooper())
}
else -> {
task.resume(null)
}
}
}

关于android - FusedLocationProviderClient 与 Kotlin 协程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55401389/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com