Android: location api asking for wifi

This is strange. I mean, I have a strong network and GPS is turned on, but it asks for Wi-Fi!

I am using a new Api location with class FusedLocationProviderClient.

Here's how I check if location settings are enabled or not:

private void checkIfLocationSettingsAreEnabled() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(5000);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);

    SettingsClient client = LocationServices.getSettingsClient(context);
    Task<LocationSettingsResponse> locationSettingsResponseTask = client.checkLocationSettings(builder.build());
    locationSettingsResponseTask.addOnSuccessListener(locationSettingsResponse -> {
        getLastKnownLocation();
    });
    locationSettingsResponseTask.addOnFailureListener(e -> {
        if (e instanceof ResolvableApiException) {
            myLocationListener.onResolutionNeeded(e);
        } else {
            myLocationListener.onLocationFailure(e);
        }
    });
}

With the code above, I can get the image below:

enter image description here

But then, after clicking OK, I call again checkIfSettingsAreEnabled(), this shows another popup, as shown below:

enter image description here

I wonder why the inclusion of Wi-Fi is mandatory, even if I am in a desert safari, where there is no Wi-Fi to connect to it.

Is there a way to Skip this option Wifiand work as usual like Google Maps?

, Google Maps PRIORITY_HIGH_ACCURACY , , , Wi-Fi.

+6
3

Wi-Fi. , , .

"" "", Activity.RESULT_CANCELLED. , Wi-Fi . , Activity.RESULT_CANCELLED, , , LocationSettingStates.

MyLocationUtils.java Base LocationActivity.java LocationFragment.java. ..!!

MyLocationUtils.java:

public class MyLocationUtils {

    public static final int REQUEST_CODE_LOCATION_SETTINGS = 123;

    private static final int INTERVAL_IN_MS = 10000;
    private static final int FASTEST_INTERVAL_IN_MS = 5000;
    private static final int MAX_WAIT_TIME_IN_MS = 5000;
    private static final int NUMBER_OF_UPDATES = 1;

    private final MyLocationListener myLocationListener;
    private final Context context;
    private FusedLocationProviderClient mFusedLocationProviderClient;
    private LocationCallback mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);

            Location location = null;
            if (locationResult.getLocations().size() > 0) {
                location = locationResult.getLocations().get(0);
            }

            myLocationListener.onLocationSuccess(location);

            stopContinuousLocation();
        }
    };

    public MyLocationUtils(Context context, MyLocationListener myLocationListener) {
        this.context = context;
        this.myLocationListener = myLocationListener;

        initializeFusedLocationProviderClient();
    }

    private boolean checkIfRequiredLocationSettingsAreEnabled(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }

    private void initializeFusedLocationProviderClient() {
        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context);
    }

    public void stopContinuousLocation() {
        mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback);
    }

    public void getLocation() {
        checkIfLocationSettingsAreEnabled();
    }

    private void checkIfLocationSettingsAreEnabled() {
        if (checkIfRequiredLocationSettingsAreEnabled(context)) {
            getLastKnownLocation();
        } else {
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
            builder.addLocationRequest(getLocationRequest());
            builder.setAlwaysShow(true);

            SettingsClient client = LocationServices.getSettingsClient(context);
            Task<LocationSettingsResponse> locationSettingsResponseTask = client.checkLocationSettings(builder.build());
            locationSettingsResponseTask.addOnSuccessListener(locationSettingsResponse -> {
                // All location settings are satisfied. The client can initialize
                // location requests here.
                // ...
                getLastKnownLocation();
            });
            locationSettingsResponseTask.addOnFailureListener(e -> {
                if (e instanceof ResolvableApiException) {
                    myLocationListener.onResolutionNeeded(e);
                } else {
                    myLocationListener.onLocationFailure(e);
                }
            });
        }
    }

    @SuppressLint("MissingPermission")
    private void getLastKnownLocation() {
        Task<Location> locationTask = mFusedLocationProviderClient.getLastLocation();
        locationTask.addOnSuccessListener(location -> {
            // Got last known location. In some rare situations this can be null.
            if (location != null) {
                myLocationListener.onLocationSuccess(location);
            } else {
                startContinuousLocation();
            }
        });
        locationTask.addOnFailureListener(myLocationListener::onLocationFailure);
    }

    @SuppressLint("MissingPermission")
    private void startContinuousLocation() {
        mFusedLocationProviderClient.requestLocationUpdates(getLocationRequest(), mLocationCallback, Looper.getMainLooper())
                .addOnFailureListener(myLocationListener::onLocationFailure);
    }

    private LocationRequest getLocationRequest() {
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(INTERVAL_IN_MS);
        locationRequest.setFastestInterval(FASTEST_INTERVAL_IN_MS);
        locationRequest.setNumUpdates(NUMBER_OF_UPDATES);
        locationRequest.setMaxWaitTime(MAX_WAIT_TIME_IN_MS);
        return locationRequest;
    }

    public void resolveLocationSettings(FragmentActivity fragmentActivity, Exception exception) {
        ResolvableApiException resolvable = (ResolvableApiException) exception;
        try {
            resolvable.startResolutionForResult(fragmentActivity, MyLocationUtils.REQUEST_CODE_LOCATION_SETTINGS);
        } catch (IntentSender.SendIntentException e1) {
            e1.printStackTrace();
        }
    }

    public interface MyLocationListener {

        void onLocationSuccess(Location location);

        void onResolutionNeeded(Exception exception);

        void onLocationFailure(Exception exception);
    }
}

BaseLocationActivity.java:

public abstract class LocationActivity extends AppCompatActivity {

    private MyLocationUtils myLocationUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myLocationUtils = new MyLocationUtils(this, new MyLocationUtils.MyLocationListener() {
            @Override
            public void onLocationSuccess(Location location) {
                if (shouldBeAllowedToProceed())
                    LocationActivity.this.onLocationSuccess(location);
            }

            @Override
            public void onResolutionNeeded(Exception exception) {
                exception.printStackTrace();
                if (shouldBeAllowedToProceed())
                    LocationActivity.this.onResolutionNeeded(exception);
            }

            @Override
            public void onLocationFailure(Exception exception) {
                exception.printStackTrace();
                if (shouldBeAllowedToProceed())
                    LocationActivity.this.onLocationFailure(exception);
            }
        });
    }

    protected void getLocation() {
        myLocationUtils.getLocation();
    }

    protected void resolveLocationSettings(Exception exception) {
        myLocationUtils.resolveLocationSettings(this, exception);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MyLocationUtils.REQUEST_CODE_LOCATION_SETTINGS) {
            final LocationSettingsStates locationSettingsStates = LocationSettingsStates.fromIntent(data);
            switch (resultCode) {
                case Activity.RESULT_OK:
                    onResolveLocationSettingOk();
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to, 
                    // or on Android Oreo 8.1 Wifi Settings were not satisfied inspite of clicking OK, that results on Activity.RESULT_CANCELED
                    onResolveLocationSettingCancelled(locationSettingsStates);
                    break;
                default:
                    break;
            }
        }
    }

    protected abstract void onResolveLocationSettingOk();

    protected void onResolveLocationSettingCancelled(LocationSettingsStates locationSettingsStates) {
        if (locationSettingsStates.isLocationPresent() && locationSettingsStates.isLocationUsable()) {
            onResolveLocationSettingOk();
        }
    }

    private boolean shouldBeAllowedToProceed() {
        return getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
    }

    public abstract void onLocationSuccess(Location location);

    public abstract void onResolutionNeeded(Exception exception);

    public abstract void onLocationFailure(Exception exception);

    @Override
    public void onDestroy() {
        super.onDestroy();
        myLocationUtils.stopContinuousLocation();
    }
}

LocationFragment.java:

public abstract class LocationFragment extends Fragment {

    private MyLocationUtils myLocationUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myLocationUtils = new MyLocationUtils(context, new MyLocationUtils.MyLocationListener() {
            @Override
            public void onLocationSuccess(Location location) {
                if (shouldBeAllowedToProceed())
                    LocationFragment.this.onLocationSuccess(location);
            }

            @Override
            public void onResolutionNeeded(Exception exception) {
                exception.printStackTrace();
                if (shouldBeAllowedToProceed())
                    LocationFragment.this.onResolutionNeeded(exception);
            }

            @Override
            public void onLocationFailure(Exception exception) {
                exception.printStackTrace();
                if (shouldBeAllowedToProceed())
                    LocationFragment.this.onLocationFailure(exception);
            }
        });
    }

    protected void resolveLocationSettings(FragmentActivity appCompatActivity, Exception exception) {
        myLocationUtils.resolveLocationSettings(appCompatActivity, exception);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MyLocationUtils.REQUEST_CODE_LOCATION_SETTINGS) {
            final LocationSettingsStates locationSettingsStates = LocationSettingsStates.fromIntent(data);
            switch (resultCode) {
                case Activity.RESULT_OK:
                    onResolveLocationSettingOk();
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    onResolveLocationSettingCancelled(locationSettingsStates);
                    break;
                default:
                    break;
            }
        }
    }

    protected abstract void onResolveLocationSettingOk();

    protected void onResolveLocationSettingCancelled(LocationSettingsStates locationSettingsStates) {
        if (locationSettingsStates.isLocationPresent() && locationSettingsStates.isLocationUsable()) {
            onResolveLocationSettingOk();
        }
    }

    public void getLocation() {
        myLocationUtils.getLocation();
    }

    private boolean shouldBeAllowedToProceed() {
        return getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED);
    }

    public abstract void onLocationSuccess(Location location);

    public abstract void onResolutionNeeded(Exception exception);

    public abstract void onLocationFailure(Exception exception);

    @Override
    public void onDestroy() {
        super.onDestroy();
        myLocationUtils.stopContinuousLocation();
    }
}
0

FusedLocationProvider GPS WIFI, . , , (GPS , ). , . GPS, GPS.

+2

PRIORITY_BALANCED_POWER_ACCURACY

This priority will come with a crude degree of accuracy that extracts location only from wi-fi, cellular network or bluetooth and NOT with GPS.

and if the device does not have a SIM card, it needs to have wi-fi in order to get the location, or it will not call onLocationChangeduntil it gets a place.

0
source

All Articles