SettingsApi
Stay organized with collections
Save and categorize content based on your preferences.
This interface is deprecated.
Use GoogleApi-based API SettingsClient
instead.
The main entry point for interacting with the location settings-enabler APIs.
This API makes it easy for an app to ensure that the device's system settings are properly
configured for the app's location needs.
When making a request to location services, the device's system settings may be in a state
that prevents an app from obtaining the location data that it needs. For example, GPS or
Wi-Fi scanning may be switched off. This intent makes it easy to:
- Determine if the relevant system settings are enabled on the device to carry out the
desired location request.
- Optionally, invoke a dialog that allows the user to enable the necessary location
settings with a single tap.
To use this API, first create a GoogleApiClient which supports at least LocationServices.API
.
Then connect the client to Google Play services:
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build()
...
mGoogleApiClient.connect();
Then create a LocationSettingsRequest.Builder
and add all of the LocationRequests
that the app will be using:
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequestHighAccuracy)
.addLocationRequest(mLocationRequestBalancedPowerAccuracy);
If the client is using BLE scans to derive location, it can request that BLE be enabled by
calling LocationSettingsRequest.Builder.setNeedBle(boolean)
:
builder.setNeedBle(true);
Then check whether current location settings are satisfied:
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleClient, builder.build());
When the PendingResult
returns, the client can check the location settings by looking at the status code from the LocationSettingsResult
object. The client can also retrieve the current state of the relevant location settings by
calling LocationSettingsResult.getLocationSettingsStates()
:
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates states = 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(
OuterClass.this,
// An arbitrary constant to disambiguate activity results.
REQUEST_CHECK_SETTINGS);
} catch (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;
}
}
});
If the status code is CommonStatusCodes.RESOLUTION_REQUIRED
, the client can call Status.startResolutionForResult(Activity, int)
to bring up a dialog, asking for
user's permission to modify the location settings to satisfy those requests. The result of
the dialog will be returned via Activity.onActivityResult(int, int, Intent)
. If the client is interested in which
location providers are available, it can retrieve a LocationSettingsStates
from the Intent
by calling LocationSettingsStates.fromIntent(Intent)
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
// All required changes were successfully made
...
break;
case Activity.RESULT_CANCELED:
// The user was asked to change settings, but chose not to
if (!state.isGpsUsable()) {
// Degrade gracefully depending on what is available
...
}
break;
default:
break;
}
break;
}
}
Public Methods
Checks if the relevant system settings are enabled on the device to carry out the
desired location requests.
Parameters
Returns
- result containing the status of the request.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License
, and code samples are licensed under the Apache 2.0 License
. For details, see the Google Developers Site Policies
. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-10-31 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-10-31 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eSettingsApi\u003c/code\u003e is deprecated; use \u003ccode\u003eSettingsClient\u003c/code\u003e for location settings-enabler APIs.\u003c/p\u003e\n"],["\u003cp\u003eThis API helps ensure device system settings are configured for an app's location needs, such as GPS or Wi-Fi.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can check and request necessary location settings using \u003ccode\u003eLocationSettingsRequest\u003c/code\u003e and handle results via \u003ccode\u003ePendingResult\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003echeckLocationSettings\u003c/code\u003e determines if location settings are enabled and if a user dialog is needed for enabling them.\u003c/p\u003e\n"]]],["The `SettingsApi` interface is deprecated; use `SettingsClient` instead. This API helps apps manage device location settings. Key actions include: building a `GoogleApiClient` and `LocationSettingsRequest`; adding `LocationRequests`; checking settings via `checkLocationSettings`; and handling the `LocationSettingsResult`. Results indicate if settings are satisfied, need user intervention (via a dialog), or are unchangeable. `LocationSettingsStates` from the `Intent` provide further provider details and `LocationSettingsRequest.Builder.setNeedBle(boolean)` can request BLE scans to be enabled.\n"],null,["# SettingsApi\n\npublic interface **SettingsApi** \n**This interface is deprecated.** \n\nUse GoogleApi-based API [SettingsClient](/android/reference/com/google/android/gms/location/SettingsClient)\ninstead.\n\nThe main entry point for interacting with the location settings-enabler APIs.\n\nThis API makes it easy for an app to ensure that the device's system settings are properly\nconfigured for the app's location needs.\n\nWhen making a request to location services, the device's system settings may be in a state\nthat prevents an app from obtaining the location data that it needs. For example, GPS or\nWi-Fi scanning may be switched off. This intent makes it easy to:\n\n- Determine if the relevant system settings are enabled on the device to carry out the desired location request.\n- Optionally, invoke a dialog that allows the user to enable the necessary location settings with a single tap.\n\nTo use this API, first create a GoogleApiClient which supports at least [LocationServices.API](/android/reference/com/google/android/gms/location/LocationServices#API).\nThen connect the client to Google Play services: \n\n```\n mGoogleApiClient = new GoogleApiClient.Builder(context)\n .addApi(LocationServices.API)\n .addConnectionCallbacks(this)\n .addOnConnectionFailedListener(this)\n .build()\n ...\n mGoogleApiClient.connect();\n```\n\nThen create a [LocationSettingsRequest.Builder](/android/reference/com/google/android/gms/location/LocationSettingsRequest.Builder)\nand add all of the [LocationRequests](/android/reference/com/google/android/gms/location/LocationRequest)\nthat the app will be using: \n\n```\n LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()\n .addLocationRequest(mLocationRequestHighAccuracy)\n .addLocationRequest(mLocationRequestBalancedPowerAccuracy);\n```\n\nIf the client is using BLE scans to derive location, it can request that BLE be enabled by\ncalling [LocationSettingsRequest.Builder.setNeedBle(boolean)](/android/reference/com/google/android/gms/location/LocationSettingsRequest.Builder#setNeedBle(boolean)): \n\n```\n builder.setNeedBle(true);\n```\n\nThen check whether current location settings are satisfied: \n\n```\n PendingResult\u003cLocationSettingsResult\u003e result =\n LocationServices.SettingsApi.checkLocationSettings(mGoogleClient, builder.build());\n```\n\nWhen the [PendingResult](/android/reference/com/google/android/gms/common/api/PendingResult)\nreturns, the client can check the location settings by looking at the status code from the\n[LocationSettingsResult](/android/reference/com/google/android/gms/location/LocationSettingsResult)\nobject. The client can also retrieve the current state of the relevant location settings by\ncalling [LocationSettingsResult.getLocationSettingsStates()](/android/reference/com/google/android/gms/location/LocationSettingsResult#getLocationSettingsStates()): \n\n```\n result.setResultCallback(new ResultCallback\u003cLocationSettingsResult\u003e() {\n @Override\n public void onResult(LocationSettingsResult result) {\n final Status status = result.getStatus();\n final LocationSettingsStates states = result.getLocationSettingsStates();\n switch (status.getStatusCode()) {\n case LocationSettingsStatusCodes.SUCCESS:\n // All location settings are satisfied. The client can initialize location\n // requests here.\n ...\n break;\n case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:\n // Location settings are not satisfied. But could be fixed by showing the user\n // a dialog.\n try {\n // Show the dialog by calling startResolutionForResult(),\n // and check the result in onActivityResult().\n status.startResolutionForResult(\n OuterClass.this,\n // An arbitrary constant to disambiguate activity results.\n REQUEST_CHECK_SETTINGS);\n } catch (SendIntentException e) {\n // Ignore the error.\n }\n break;\n case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:\n // Location settings are not satisfied. However, we have no way to fix the\n // settings so we won't show the dialog.\n ...\n break;\n }\n }\n });\n```\n\nIf the status code is [CommonStatusCodes.RESOLUTION_REQUIRED](/android/reference/com/google/android/gms/common/api/CommonStatusCodes#RESOLUTION_REQUIRED), the client can call [Status.startResolutionForResult(Activity, int)](/android/reference/com/google/android/gms/common/api/Status#startResolutionForResult(android.app.Activity,%20int)) to bring up a dialog, asking for\nuser's permission to modify the location settings to satisfy those requests. The result of\nthe dialog will be returned via [Activity.onActivityResult(int, int, Intent)](//developer.android.com/reference/android/app/Activity.html#onActivityResult(int,%20int,%20android.content.Intent)). If the client is interested in which\nlocation providers are available, it can retrieve a [LocationSettingsStates](/android/reference/com/google/android/gms/location/LocationSettingsStates)\nfrom the [Intent](//developer.android.com/reference/android/content/Intent.html) by calling\n[LocationSettingsStates.fromIntent(Intent)](/android/reference/com/google/android/gms/location/LocationSettingsStates#fromIntent(android.content.Intent)): \n\n```\n @Override\n protected void onActivityResult(int requestCode, int resultCode, Intent data) {\n final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);\n switch (requestCode) {\n case REQUEST_CHECK_SETTINGS:\n switch (resultCode) {\n case Activity.RESULT_OK:\n // All required changes were successfully made\n ...\n break;\n case Activity.RESULT_CANCELED:\n // The user was asked to change settings, but chose not to\n if (!state.isGpsUsable()) {\n // Degrade gracefully depending on what is available\n ...\n }\n break;\n default:\n break;\n }\n break;\n }\n }\n``` \n\n### Public Method Summary\n\n|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract [PendingResult](/android/reference/com/google/android/gms/common/api/PendingResult)\\\u003c[LocationSettingsResult](/android/reference/com/google/android/gms/location/LocationSettingsResult)\\\u003e | [checkLocationSettings](/android/reference/com/google/android/gms/location/SettingsApi#checkLocationSettings(com.google.android.gms.common.api.GoogleApiClient,%20com.google.android.gms.location.LocationSettingsRequest))([GoogleApiClient](/android/reference/com/google/android/gms/common/api/GoogleApiClient) client, [LocationSettingsRequest](/android/reference/com/google/android/gms/location/LocationSettingsRequest) locationSettingsRequest) Checks if the relevant system settings are enabled on the device to carry out the desired location requests. |\n\nPublic Methods\n--------------\n\n#### public abstract [PendingResult](/android/reference/com/google/android/gms/common/api/PendingResult)\\\u003c[LocationSettingsResult](/android/reference/com/google/android/gms/location/LocationSettingsResult)\\\u003e\n**checkLocationSettings** ([GoogleApiClient](/android/reference/com/google/android/gms/common/api/GoogleApiClient) client, [LocationSettingsRequest](/android/reference/com/google/android/gms/location/LocationSettingsRequest) locationSettingsRequest)\n\nChecks if the relevant system settings are enabled on the device to carry out the\ndesired location requests. \n\n##### Parameters\n\n| client | an existing GoogleApiClient. It does not need to be connected at the time of this call, but the result will be delayed until the connection is complete. |\n| locationSettingsRequest | an object that contains all the location requirements that the client is interested in. |\n|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- result containing the status of the request."]]