简体   繁体   English

requestLocationUpdates 不调用 locationCallback

[英]requestLocationUpdates doesn't call the locationCallback

I started to make a Kotlin app and i'm trying to access the users location.我开始制作 Kotlin 应用程序,我正在尝试访问用户位置。 It's all my app must do.这是我的应用程序必须做的所有事情。 However, the function "client.requestLocationUpdates(locationRequest, locationCallback, null)" never calls my locationCallback method and I don't know why.但是,function "client.requestLocationUpdates(locationRequest, locationCallback, null)" 从不调用我的 locationCallback 方法,我不知道为什么。 I already debug this code, it execute the "client.requestLocationUpdates(locationRequest, locationCallback, null)" line but it never execute my LocationCallbackFunction.我已经调试了这段代码,它执行了“client.requestLocationUpdates(locationRequest,locationCallback,null)”行,但它从不执行我的 LocationCallbackFunction。 Here's my code:这是我的代码:

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

}

That is the entire content of my activity这就是我活动的全部内容

I solved this, and I don't know how, this exactly code is running perfectly right now.我解决了这个问题,但我不知道如何,这段代码现在运行得很好。 It was a error in my Android Studio configuration, I guess我猜这是我的 Android Studio 配置中的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LocationManager requestLocationUpdates不起作用 - LocationManager requestLocationUpdates doesn't work requestLocationUpdates()不适用于真正的智能手机 - requestLocationUpdates() doesn't work on a real smartphone Android locationManager requestLocationUpdates不起作用 - Android locationManager requestLocationUpdates doesn't work locationClient.requestLocationUpdates不会触发Kitkat中的Pending Intent - locationClient.requestLocationUpdates doesn't fires Pending Intent in Kitkat Android:LocationServices.FusedLocationApi.requestLocationUpdates继续运行且不返回 - Android: LocationServices.FusedLocationApi.requestLocationUpdates keep running and doesn't return Android:LocationManager.requestLocationUpdates不尊重params - Android: LocationManager.requestLocationUpdates doesn't respect params addProximityAlert不起作用(requestLocationUpdates也不起作用) - Android - addProximityAlert doesn't work (neither does requestLocationUpdates) - Android 调用requestLocationUpdates()时发生IllegalStateException - IllegalStateException on call of requestLocationUpdates() 何时requestLocationUpdates调用? - When requestLocationUpdates call? 如何在线程中调用requestLocationUpdates? - How call requestLocationUpdates in thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM