public void setUpLocationRequest(OnSuccessListener<LocationSettingsResponse> successListener, OnFailureListener onFailureListener) { LocationRequest locationRequest = getLocationRequest(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); SettingsClient settingsClient = LocationServices.getSettingsClient(context); Task<LocationSettingsResponse> locationSettingsResponseTask = settingsClient.checkLocationSettings(builder.build()); locationSettingsResponseTask.addOnSuccessListener(successListener); locationSettingsResponseTask.addOnFailureListener(onFailureListener); }
private void setupLocationService() { Log.v(TAG, "Setting up location service"); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(getLocationRequest()); SettingsClient client = LocationServices.getSettingsClient(activity); Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build()); task.addOnSuccessListener(activity, new OnSuccessListener<LocationSettingsResponse>() { @Override public void onSuccess(LocationSettingsResponse locationSettingsResponse) { Log.v(TAG, "Location settings satisfied"); } }); task.addOnFailureListener(activity, new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { int statusCode = ((ApiException) e).getStatusCode(); switch (statusCode) { case CommonStatusCodes.RESOLUTION_REQUIRED: Log.w(TAG, "Location settings not satisfied, attempting resolution intent"); try { ResolvableApiException resolvable = (ResolvableApiException) e; resolvable.startResolutionForResult(activity, REQUEST_CODE_LOCATION_SETTINGS); } catch (IntentSender.SendIntentException sendIntentException) { Log.e(TAG, "Unable to start resolution intent"); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.w(TAG, "Location settings not satisfied and can't be changed"); break; } } }); }
/** * Method to check for the location settings of the device, * if it doesn't fit the current requirement of the app * open the settings for user to change them, if the settings are OK, starts the TimeService. */ @Override public void startLocationService() { LocationRequest request = new LocationRequest() .setFastestInterval(1500) .setInterval(3000) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); LocationSettingsRequest.Builder settingsRequest = new LocationSettingsRequest.Builder(); settingsRequest.addLocationRequest(request); SettingsClient client = LocationServices.getSettingsClient(getActivity()); Task<LocationSettingsResponse> responseTask = client.checkLocationSettings (settingsRequest.build()); responseTask.addOnSuccessListener(getActivity(), locationSettingsResponse -> locationScheduler()); responseTask.addOnFailureListener(getActivity(), e -> { int statusCode = ((ApiException) e).getStatusCode(); switch (statusCode){ case CommonStatusCodes.RESOLUTION_REQUIRED: try { ResolvableApiException apiException = ((ResolvableApiException)e); apiException.startResolutionForResult(getActivity(), permissionCode); Log.d(TAG, "Dialog displayed"); }catch (IntentSender.SendIntentException sendIntentException){ Log.d(TAG, "Error displaying dialogBox", sendIntentException); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.d(TAG, "Unable to turn on location service", e); } }); }