use of com.google.android.gms.location.SettingsClient in project Instafood by Gear61.
the class LocationManager method checkLocationServicesAndFetchLocationIfOn.
private void checkLocationServicesAndFetchLocationIfOn() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
SettingsClient client = LocationServices.getSettingsClient(activity);
client.checkLocationSettings(builder.build()).addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
fetchAutomaticLocation();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
if (exception instanceof ResolvableApiException) {
locationServicesManager.askForLocationServices(LOCATION_SERVICES_CODE);
} else {
onLocationFetchFail();
}
}
});
}
use of com.google.android.gms.location.SettingsClient in project routerkeygenAndroid by routerkeygen.
the class NetworksListActivity method settingsRequest.
public static void settingsRequest(final Activity activity, OnSuccessListener cb) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(100000);
mLocationRequest.setFastestInterval(50000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);
SettingsClient client = LocationServices.getSettingsClient(activity);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(activity, cb);
task.addOnFailureListener(activity, e -> {
int statusCode = ((ApiException) e).getStatusCode();
switch(statusCode) {
case CommonStatusCodes.RESOLUTION_REQUIRED:
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(activity, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// to fix the settings so we won't show the dialog.
break;
}
});
}
use of com.google.android.gms.location.SettingsClient in project IITB-App by wncc.
the class MapFragment method setupGPS.
public void setupGPS(boolean showWarning) {
if (getView() == null || getActivity() == null)
return;
// Permissions stuff
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_PERMISSIONS_REQUEST_LOCATION);
} else {
try {
// Create the location request to start receiving updates
mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(500);
mLocationRequest.setFastestInterval(200);
// Create LocationSettingsRequest object using location request
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
// Check whether location settings are satisfied
SettingsClient settingsClient = LocationServices.getSettingsClient(getActivity());
settingsClient.checkLocationSettings(locationSettingsRequest);
// Setup the callback
myLocationCallback = new MyLocationCallback();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, myLocationCallback, Looper.myLooper());
GPSIsSetup = true;
if (showWarning) {
Toast.makeText(getContext(), "WARNING: Location is in Beta. Use with Caution.", Toast.LENGTH_LONG).show();
}
} catch (SecurityException ignored) {
Toast.makeText(getContext(), "No permission!", Toast.LENGTH_LONG).show();
}
}
}
use of com.google.android.gms.location.SettingsClient in project coursera-android by aporter.
the class LocationGetLocationActivity method continueAcquiringLocations.
private void continueAcquiringLocations() {
// Start location services
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(POLLING_FREQ);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_FREQ);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
// Used if needed to turn on settings related to location services
SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize location requests here.
if (checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationCallback = getLocationCallback();
mLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
// Schedule a runnable to stop location updates after a period of time
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
@Override
public void run() {
mLocationClient.removeLocationUpdates(mLocationCallback);
}
}, MEASURE_TIME, TimeUnit.MILLISECONDS);
}
}
});
task.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch(statusCode) {
case CommonStatusCodes.RESOLUTION_REQUIRED:
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(LocationGetLocationActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// to fix the settings so we won't show the dialog.
break;
}
}
});
}
Aggregations