2022-10-28

ClassCastException: ApiException cannot be cast to RevolvableApiException after updating location library to 21 version

I have updated location services libraries in my App to the latest 21 version:
com.google.android.gms:play-services-location:21.0.0
and it breaks the logic for enabling location settings on the users phones.

I found updated page with documentation about this process: https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient,
and using code below for triggering popup which should ask user to allow enabling location access on the phone:

val locationRequest = LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, 10000)
                .setMinUpdateIntervalMillis(5000).build()

            val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)

            val client: SettingsClient = LocationServices.getSettingsClient(activity)
            val task: Task<LocationSettingsResponse> = client.checkLocationSettings(builder.build())
            task.addOnCompleteListener {
                try {
                    task.getResult(ApiException::class.java)
                } catch (exception: ApiException) {
                    when (exception.statusCode) {
                        LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
                            try {
                                val resolvable = exception as ResolvableApiException
                                resolvable.startResolutionForResult(
                                    activity,
                                    1
                                )
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                        LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> {
                           
                        }
                    }
                }
            }

but that code from documentation throw a ClassCastException on this line:
val resolvable = exception as ResolvableApiException,
can't figure out how to deal with a latest location updates, thanks in advance for any help.



No comments:

Post a Comment