requestLocationUpdates没有调用locationCallback。

问题描述 投票:1回答:1

我开始制作一个Kotlin应用程序,我试图访问用户的位置。这是我的应用程序必须要做的事情,但是 "client.requestLocationUpdates(locationRequest, locationCallback, null) "这个函数从来没有调用过我的locationCallback。但是,"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)
    }

}

这就是我的活动的全部内容

android kotlin geolocation location locationmanager
1个回答
0
投票

我解决了这个问题,我也不知道怎么解决的,这个具体代码现在运行得很完美。这是一个错误,在我的Android Studio配置,我猜。

© www.soinside.com 2019 - 2024. All rights reserved.