androidgoogle-play-servicescurrentlocation

is location detection required google play services android studio


I have built an app using location detection to get the current location I am using LocationManager and LocationLiastener

the app works fine but when I tried to run it on advice without android play service it won't get the current Location and show the following dialog enter image description here

this is the app link on Github https://github.com/mostafa-n3ma/Pcm-helper1.0


Solution

  • this is the problem

        private fun checkDeviceLocationSettingsAndGetLocationUpdates(resolve: Boolean = true) {
        val locationRequest = com.google.android.gms.location.LocationRequest.create().apply {
            priority = LocationRequest.QUALITY_LOW_POWER
        }
        val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
    
        val settingsClint = LocationServices.getSettingsClient(requireActivity())
        val locationSettingsResponseTask = settingsClint.checkLocationSettings(builder.build())
    
        locationSettingsResponseTask.addOnFailureListener { exeption ->
            if (exeption is ResolvableApiException && resolve) {
                try {
                    startIntentSenderForResult(
                        exeption.resolution.intentSender,
                        REQUEST_TURN_DEVICE_LOCATION_ON,
                        null,
                        0, 0, 0,
                        null
                    )
                } catch (sendEx: IntentSender.SendIntentException) {
                    Log.d(TAG, "Error geting location settings resolution: " + sendEx.message)
                }
            } else {
                Snackbar.make(
                    this.requireView(),
                    "Location services must be enabled to use the app", Snackbar.LENGTH_INDEFINITE
                ).setAction(android.R.string.ok) {
                    checkDeviceLocationSettingsAndGetLocationUpdates()
                }.show()
            }
    
        }
    
        locationSettingsResponseTask.addOnCompleteListener {
            if (it.isSuccessful) {
                locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 0, 0.0f, locationListener
                )
    
            }
        }
       
    
    }
    

    I am using the google services library to check Location setting if it was enabled or not if the location is ON the location update request will be triggered

    else it will ask the user to enable it and then trigger the location update request

    if the services are not available nothing will happen and no location update will be requested

    so I will try another way to check the location feature