在Sony Xperia Z2的onLocationAvailability回调中,FusedLocationProviderClient始终返回false

时间:2018-10-11 11:13:57

标签: kotlin fusedlocationproviderapi

FusedLocationProviderClient始终在Sony Xperia Z2的onLocationAvailability回调中返回false。授予权限,启用位置。在位置首选项中,设置选项以使用wifi,网络和gps。在模拟器,三星,BQ,Nexus,小米上一切正常。操作系统版本从6到8。但是在Sony(andorid 6.0.1)上,我遇到了这个问题。有任何想法吗 ?我结束了

回调

private val mUserLocationCallback = object : LocationCallback() {

    override fun onLocationAvailability(locationAvailability: LocationAvailability?) {
        super.onLocationAvailability(locationAvailability)
        val isAvalable = locationAvailability?.isLocationAvailable ?: false
        if (!isAvalable) {
            mLocationHelperListener.onLocationUnAvailable()
            mDefaulLocation?.let { mLocationHelperListener.onLocationHelperChanged(it) }
            stopLocationUpdates()
        }
    }

    override fun onLocationResult(locationResult: LocationResult?) {

        if (locationResult != null) {
            for (location in locationResult.locations) {
                mPreferences?.lastLatitude = location.latitude.toFloat()
                mPreferences?.lastLongitude = location.longitude.toFloat()
                mLocationHelperListener.onLocationHelperChanged(location)
                stopLocationUpdates()
                break
            }
        }

    }
}

请求

val locationRequest = LocationRequest.create()
                    .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                    .setInterval(UPDATE_INTERVAL)
                    .setFastestInterval(FASTEST_INTERVAL)
                    .setSmallestDisplacement(SMALLEST_DISTANCE)


            // Create LocationSettingsRequest object using location request
            val builder = LocationSettingsRequest.Builder()
            builder.addLocationRequest(locationRequest)
            val locationSettingsRequest = builder.build()

            val settingsClient = LocationServices.getSettingsClient(mContext)
            val task = settingsClient.checkLocationSettings(locationSettingsRequest)
            task.addOnSuccessListener { locationSettingsResponse: LocationSettingsResponse? ->
                mFusedLocationClient.requestLocationUpdates(locationRequest, mUserLocationCallback, null)
            }
            task.addOnFailureListener { exception: Exception ->
                mLocationHelperListener.onLocationUnAvailable()
            }

2 个答案:

答案 0 :(得分:0)

我有一个非常相似的问题;重新启动手机解决了问题。我正在数台设备上进行测试,但唯一有问题的设备是ZTE Maven Z812。

答案 1 :(得分:-1)

我认为找到了解决方案。我认为我的解决方案适用于存在相同问题的其他设备。所以... 转到应用程序列表,找到Google PLay Services。打开权限。如果“位置”权限已关闭,请打开它。 如果没有此类项目,请关闭所有项目,然后再次打开。您应该看到“位置”权限对话框。启用它,瞧,我现在一切正常。