Created
April 3, 2025 19:05
-
-
Save Zulqurnain/53bd7fae3ad1d4dfa3588eb6cfcc478e to your computer and use it in GitHub Desktop.
Location Permission Issue resolve
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun handleLocationAccess( | |
activity: ComponentActivity, | |
fusedLocationClient: FusedLocationProviderClient, | |
onLocationReady: (Location) -> Unit, | |
onFailure: (String) -> Unit | |
) { | |
val permissionLauncher = activity.registerForActivityResult( | |
ActivityResultContracts.RequestPermission() | |
) { isGranted -> | |
if (isGranted) { | |
proceedWithLocation(activity, fusedLocationClient, onLocationReady, onFailure) | |
} else { | |
onFailure("Permission denied by user") | |
} | |
} | |
when { | |
ContextCompat.checkSelfPermission( | |
activity, | |
Manifest.permission.ACCESS_FINE_LOCATION | |
) == PackageManager.PERMISSION_GRANTED -> { | |
proceedWithLocation(activity, fusedLocationClient, onLocationReady, onFailure) | |
} | |
ActivityCompat.shouldShowRequestPermissionRationale( | |
activity, | |
Manifest.permission.ACCESS_FINE_LOCATION | |
) -> { | |
onFailure("Location permission is required to use this feature.") | |
permissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION) | |
} | |
else -> { | |
permissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION) | |
} | |
} | |
} | |
private fun proceedWithLocation( | |
context: Context, | |
fusedLocationClient: FusedLocationProviderClient, | |
onSuccess: (Location) -> Unit, | |
onFailure: (String) -> Unit | |
) { | |
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager | |
val isLocationEnabled = | |
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || | |
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) | |
if (!isLocationEnabled) { | |
onFailure("Location services are turned off.") | |
context.startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)) | |
return | |
} | |
try { | |
fusedLocationClient.lastLocation | |
.addOnSuccessListener { location -> | |
if (location != null) { | |
onSuccess(location) | |
} else { | |
onFailure("Failed to get location. Try again.") | |
} | |
} | |
.addOnFailureListener { | |
onFailure("Error retrieving location: ${it.localizedMessage}") | |
} | |
} catch (e: SecurityException) { | |
onFailure("Security exception: ${e.localizedMessage}") | |
} | |
} | |
//usage | |
/* | |
val fusedClient = LocationServices.getFusedLocationProviderClient(this) | |
handleLocationAccess( | |
activity = this, | |
fusedLocationClient = fusedClient, | |
onLocationReady = { location -> | |
Log.d("Location", "Lat: ${location.latitude}, Lng: ${location.longitude}") | |
}, | |
onFailure = { error -> | |
Toast.makeText(this, error, Toast.LENGTH_SHORT).show() | |
} | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment