【问题标题】:requestLocationUpdates doesn't call the locationCallbackrequestLocationUpdates 不调用 locationCallback
【发布时间】:2020-05-18 00:58:11
【问题描述】:

我开始制作 Kotlin 应用程序,并且正在尝试访问用户的位置。这是我的应用程序必须做的所有事情。但是,函数“client.requestLocationUpdates(locationRequest, locationCallback, null)”从不调用我的 locationCallback 方法,我也不知道为什么。我已经调试了这段代码,它执行了“client.requestLocationUpdates(locationRequest,locationCallback,null)”行,但它从不执行我的 LocationCallbackFunction。这是我的代码:

class MainActivity : AppCompatActivity() {
    private lateinit var client : FusedLocationProviderClient
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        client = LocationServices.getFusedLocationProviderClient(this)
    }


    override fun onResume() {
        super.onResume()
        var codigo = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
        codigo = 0
        when (codigo) {
            ConnectionResult.SERVICE_MISSING,
            ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED,
            ConnectionResult.SERVICE_DISABLED -> {
                GoogleApiAvailability.getInstance().getErrorDialog(this, codigo, 0, DialogInterface.OnCancelListener {
                    fun onCancel(dialog: DialogInterface?) {
                        finish();
                    }
                }).show()
            }
            ConnectionResult.SUCCESS -> {
                println("Google Play Service is working!")
            }
        }

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            println("This app hasn't permission to access users location")
            return;
        }


        val locationRequest = LocationRequest.create()
        val fifteen = 15 * 1000.toLong()
        val five = 5 * 1000.toLong()
        locationRequest.interval = fifteen
        locationRequest.fastestInterval = five
        locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        var builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
        var settingsClient = LocationServices.getSettingsClient(this)
        settingsClient.checkLocationSettings(builder.build())
            .addOnSuccessListener(
                fun (locationSettingsResponse : LocationSettingsResponse) {
                    println("locationSettingsResponse.locationSettingsStates.isGpsPresent = ${locationSettingsResponse.locationSettingsStates.isGpsPresent}")
                    println("locationSettingsResponse.locationSettingsStates.isGpsUsable = ${locationSettingsResponse.locationSettingsStates.isGpsUsable}")

                }
            )
            .addOnFailureListener(

                fun (e: Exception) : Unit {
                    if (e is ResolvableApiException) {
                        try {
                            var resolvable : ResolvableApiException = e
                            resolvable.startResolutionForResult(this, 10);
                        } catch (e1 : IntentSender.SendIntentException ) {
                        }
                    }
                }
            )


        val locationCallback : LocationCallback? = object : LocationCallback() {
            override fun onLocationResult(locationResult : LocationResult?) {
                if (locationResult == null) {
                    println("the value is null")
                    return;
                }

                for ( location : Location in locationResult.getLocations()) {
                    location.getLatitude()
                    println("latitude=" + location.getLatitude());
                    textoPrincipal.text = location.getLatitude().toString()
                }
            }
        }

        client.requestLocationUpdates(locationRequest, locationCallback, null)
    }

}

这就是我活动的全部内容

【问题讨论】:

  • checkLocationSettings 成功时应请求LocationUpdates,并确保打开位置服务并请求所有位置权限
  • 我尝试将 requestLocationUpdates 放入 CheckLocationSettings 成功,但它不起作用。我认为我的代码已经将“.addOnFailureListener”中的所有位置权限请求到“settingsClient.checkLocationSettings(builder.build())”

标签: android kotlin geolocation location locationmanager


【解决方案1】:

我解决了这个问题,但我不知道如何,这段代码现在运行得很好。我猜这是我的 Android Studio 配置中的错误

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2020-10-11
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    • 2012-06-10
    • 1970-01-01
    相关资源
    最近更新 更多