Search code examples
androidgpswifi

Android Settings API turn on WiFi to improve location accuracy if GPS is already turned on


I have use new Settings API to turn on GPS without to leave my application. http://android-developers.blogspot.in/2015/03/google-play-services-70-places-everyone.html I have this locationRequest with PRIORITY_HIGH_ACCURACY to use GPS.

final LocationRequest locationRequest = LocationRequest.create() 
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(DriverApplication.TRACE_MAX_INTERVAL)
            .setMaxWaitTime(DriverApplication.TRACE_MAX_INTERVAL*2)
            .setFastestInterval(DriverApplication.TRACE_FASTEST_INTERVAL);

but GMS Location not use GPS only it appears message to turn on WiFi too. I have try to remove forget all WiFi networks and message from GMS is changed and adding request from google to collect anonymous location data and send it.. How to skip this message if WiFi is off?

From LocationRequest documentation:

The priority of the request is a strong hint to the LocationClient for which location sources to use. For example, PRIORITY_HIGH_ACCURACY is more likely to use GPS, and PRIORITY_BALANCED_POWER_ACCURACY is more likely to use WIFI & Cell tower positioning, but it also depends on many other factors (such as which sources are available) and is implementation dependent.

PRIORITY_HIGH_ACCURACY is more likely to use GPS - if GPS is enable why its need to send user message to enable WiFi? and this not depend of the accuracy of the GPS coordinates. Every time if GPS is enabled Settings API show dialog to enable WiFi.


Solution

  • See LocationSettingsStates.isGpsUsable().

    LocationSettingsStates locationSettingsStates = locationSettingsResult.getLocationSettingsStates();
        if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {
                Status status = locationSettingsResult.getStatus();
            if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) {
                try {
                    status.startResolutionForResult(StilActivity.this, REQUEST_CHECK_SETTINGS);
                }
                catch (IntentSender.SendIntentException th) {
                    Log.e(TAG, "Error opening settings activity.", th);
                }
            }
        }