1

I have a code that makes location requests for the user, everything works fine as long as the user is already in the "location" mode is active, however, if the "location" mode is not activated in the device notification bar location returns null, I know that when we disable the option the cache of locations is cleared, how could I ask the user to activate the option for q then I request the getLastLocation?

fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
                @Override
                public void onComplete(@NonNull Task<Location> task) {
                    localidade = task.getResult();
                    if(localidade != null){
                        try {
                            locFromLatLon = new Geocoder(AddAnuncioActivity.this, Locale.getDefault());
                            List<Address> addresses = locFromLatLon.getFromLocation(localidade.getLatitude(), localidade.getLongitude(), 1);
                            Log.i("VALORZAS", addresses.get(0).getCountryName());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else{
// Put some here whenlocation is null -- this happens when the Location option in notification bar of // user is unabled
                    }
                }
            });

This only work if the option from the image is enabled, if this option is off, the getResult returns null. locality enabled

My onResume function:

    @Override
    protected void onResume() {
        super.onResume();
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(60000);
        locationRequest.setFastestInterval(5000);
        LocationCallback locationCallback = new LocationCallback(){
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    Toast.makeText(getApplicationContext(), "Localidade ainda vazia", Toast.LENGTH_LONG).show();
                    return;
                }
                for (Location location : locationResult.getLocations()) {

                }
            }
        };
        fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null);
    }
luke cross
  • 321
  • 1
  • 2
  • 19

1 Answers1

1

You can try with LocationManager:

final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
    //Call Alert or use android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS

}

Refer the Documentation

Lakshitha Wisumperuma
  • 1,574
  • 1
  • 10
  • 17