NitZRobotKoder
NitZRobotKoder

Reputation: 1086

Android :Enable location settings programatically with out leaving app or going to settings screen

Maybe already answered, but is there official apis to Enable location settings programatically with out going to settings screen.

This does not take to settings screen but enables locationwhat are best approach which works from 2.3 and above android version

Thanks

NitZ

Upvotes: 0

Views: 2156

Answers (3)

user7265840
user7265840

Reputation: 1

googleApiClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

add the above line of code before the end of locationChecker().

Upvotes: 0

Srishti Roy
Srishti Roy

Reputation: 576

This will help

public void locationChecker(GoogleApiClient mGoogleApiClient, final Activity activity) {
            LocationRequest locationRequest = LocationRequest.create();
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            locationRequest.setInterval(30 * 1000);
            locationRequest.setFastestInterval(5 * 1000);
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
            builder.setAlwaysShow(true);
            PendingResult<LocationSettingsResult> result =
                    LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());
            result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
                @Override
                public void onResult(LocationSettingsResult result) {
                    final Status status = result.getStatus();
                    final LocationSettingsStates state = 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(
                                        activity, 1000);
                            } catch (IntentSender.SendIntentException e) {
                                // Ignore the error.
                            }
                            break;
                        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                            // Location settings are not satisfied. However, we have no way to fix the
                            // settings so we won't show the dialog.
                            break;
                    }
                }
            }

);
    }

Upvotes: 1

NitZRobotKoder
NitZRobotKoder

Reputation: 1086

Well found this in google play services 7.0...fusedapiprovider/settingsapi are api to be used.. http://android-developers.blogspot.in/2015/03/google-play-services-70-places-everyone.html?m=1 https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi

Happy coding..

Upvotes: 0

Related Questions