带有Kotlin协同程序的融合位置提供者客户端

ntjbwcob  于 2022-11-30  发布在  Kotlin
关注(0)|答案(3)|浏览(126)

我正在尝试使用FusedLocationProviderClient和Kotlin协同程序请求新位置。这是我的当前设置:

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()),这是否意味着我只能调用该函数一次?
任何帮助都是感激不尽的。

y1aodyip

y1aodyip1#

问题出在您设定coroutineContext的方式上。请改用下列方式:

override val coroutineContext = Dispatchers.Main + job

如果需要IO调度程序,可以显式地要求它:

withContext(Dispatchers.IO) { ... blocking IO code ... }

要暂停协程,请调用suspendCancellableCoroutine,否则您将无法从结构化并发中获得任何好处。
另一个细节是,不要在suspendCancellableCoroutine块中的it.resume之后写任何代码。如果调度程序选择立即恢复协程,在resume调用中,该代码将不会执行,直到协程的所有代码都运行完(或者至少直到下一个挂起点)。

override fun onLocationResult(result: LocationResult) {
    fusedLocationProviderClient.removeLocationUpdates(this)
    it.resume(result.lastLocation.run { latitude to longitude })
}
0md85ypi

0md85ypi2#

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)
        }
    }
}
hwazgwia

hwazgwia3#

通过使用suspendCoroutine,您可以在运行时调用挂起函数的调度程序上调用提供的代码。由于大多数调度程序不在循环线程上运行(几乎只有Dispatchers.MAIN在循环线程上运行),因此对Looper.myLooper()的调用失败。
文档中说你可以用null代替Looper.myLooper()来调用一个未指定线程上的回调函数,然后内置的协程调度器会确保它被路由到正确的线程以恢复执行。
编辑:你可能需要调用it.intercepted().resume(latLng)来确保结果被分派到正确的线程。我不完全确定suspendCoroutine的延续是否被默认拦截。
此外,您不需要调用fusedLocationProviderClient.removeLocationUpdates(this),因为您已经将LocationRequest中的更新次数设置为1

相关问题