Android: Czy Mogę włączyć GPS bez przekierowywania użytkownika do ekranu ustawień, jak w aplikacji "google maps"

W aplikacjach opartych na GPS ważne jest, aby użytkownik włączył swój GPS. Jeśli nie, zazwyczaj pojawi się okno dialogowe z informacją, że użytkownik " powinien włączyć swój GPS z ustawień, aby móc korzystać z tej funkcji".

Gdy użytkownik naciśnie OK zostanie przekierowany na stronę ustawień, nie podoba mi się to rozwiązanie, ponieważ zabiera użytkownika z kontekstu aplikacji w Ustawienia.

Zauważyłem, że aplikacja "google maps" ma lepsze rozwiązanie, co ma pokazać schludne okno dialogowe, gdy potrzebna jest funkcja GPS. Po wybraniu przez użytkownika "OK" GPS zostanie włączony bezpośrednio bez żadnego przekierowania do ustawień.

Czy Mogę włączyć GPS bez przekierowywania użytkownika do ekranu ustawień, jak w aplikacji "google maps"?

Sprawdź poniższy obrazek:

Schludne Okno Dialogowe

Author: grebulon, 2015-04-20

1 answers

Aby mieć tę funkcję potrzebujesz:

  • najpierw (przynajmniej) Wersja 7.0 usług play

compile 'com.google.android.gms:play-services:7.0.0'

  • drugie coś takiego w Twoim kodzie (miałem to w moim onCreate):

-

 // Check the location settings of the user and create the callback to react to the different possibilities
LocationSettingsRequest.Builder locationSettingsRequestBuilder = new LocationSettingsRequest.Builder()
                .addLocationRequest(mLocationRequest);
PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, locationSettingsRequestBuilder.build());
result.setResultCallback(mResultCallbackFromSettings);

A następnie utworzyć wywołanie zwrotne:

// The callback for the management of the user settings regarding location
private ResultCallback<LocationSettingsResult> mResultCallbackFromSettings = new ResultCallback<LocationSettingsResult>() {
    @Override
    public void onResult(LocationSettingsResult result) {
        final Status status = result.getStatus();
        //final LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();
        switch (status.getStatusCode()) {
            case LocationSettingsStatusCodes.SUCCESS:
                // All location settings are satisfied. The client can initialize location
                // requests here.
                break;
            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                // Location settings are not satisfied. But could be fixed by showing the user
                // a dialog.
                try {
                    // Show the dialog by calling startResolutionForResult(),
                    // and check the result in onActivityResult().
                    status.startResolutionForResult(
                            MapActivity.this,
                            REQUEST_CHECK_SETTINGS);
                } catch (IntentSender.SendIntentException e) {
                    // Ignore the error.
                }
                break;
            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                Log.e(TAG, "Settings change unavailable. We have no way to fix the settings so we won't show the dialog.");
                break;
        }
    }
};

I w końcu w onActivityResult miałem:

/**
 * Used to check the result of the check of the user location settings
 *
 * @param requestCode code of the request made
 * @param resultCode code of the result of that request
 * @param intent intent with further information
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(intent);
    switch (requestCode) {
        case REQUEST_CHECK_SETTINGS:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // All required changes were successfully made
                    if (mGoogleApiClient.isConnected() && userMarker == null) {
                        startLocationUpdates();
                    }
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    break;
                default:
                    break;
            }
            break;
    }
}
 20
Author: AlvaroSantisteban,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-20 10:00:12