given my source code i am try to getting latitude and longitude after enable gps but getting error fusedLocationProvider failure executes why it happen i am not understood and getting this error uid 10613 does not have any of [android.permission.ACCESS_FINE_LOCATION, android.permission.ACCESS_COARSE_LOCATION].
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onStart() {
super.onStart();
requestPermission();
}
@RequiresApi(api = Build.VERSION_CODES.P)
private void requestPermission() {
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
getLiveLocation();
}
else{
getPermission();
}
}
private void getPermission() {
locationRequest = com.google.android.gms.location.LocationRequest.create()
.setPriority(com.google.android.gms.location.LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10000)
.setFastestInterval(5000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addAllLocationRequests(Collections.singleton(locationRequest));
builder.setAlwaysShow(true);
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(requireActivity())
.checkLocationSettings(builder.build());
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
Toast.makeText(requireActivity(), "Gps on", Toast.LENGTH_SHORT).show();
getLiveLocation();
} catch (ApiException e) {
switch (e.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvableApiException = (ResolvableApiException) e;
resolvableApiException.startResolutionForResult(requireActivity(), REQUEST_CODE);
} catch (IntentSender.SendIntentException ex) {
break;
}
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
}
});
}
@SuppressLint("MissingPermission")
@RequiresApi(api = Build.VERSION_CODES.P)
private void getLiveLocation() {
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(requireActivity(), new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
// Got last known location
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(requireContext(),
"Latitude: " + latitude + ", Longitude: " + longitude,
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(requireContext(),
"Location not available", Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e("errors",e.getLocalizedMessage());
Toast.makeText(requireActivity(), ""+e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE){
switch (requestCode){
case Activity.RESULT_OK:
Toast.makeText(requireActivity(), "gps turn on", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getLiveLocation();
}
break;
case Activity.RESULT_CANCELED:
Toast.makeText(requireActivity(), "gps is required", Toast.LENGTH_SHORT).show();
}
}
}
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onResume() {
super.onResume();
allocation.setOnClickListener(v -> {
isOn=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(isOn){
getLiveLocation();
}
else{
requestPermission();
}
Toast.makeText(requireActivity(), "work", Toast.LENGTH_SHORT).show();
});
}
i am try to getting latitude and longitude after enable gps but getting error fusedLocationProvider failure executes and implement techniques but not getting this result uid 10613 does not have any of [android.permission.ACCESS_FINE_LOCATION, android.permission.ACCESS_COARSE_LOCATION]. but when implement activity that time i successfully getting latitude and longitude but when i am working my project for using getting latitude and longitude its not properly done and fail
It is because you need to add permissions for location to get current location. You can do it as follows.
Just add these two lines in your manifest file