use of com.google.android.gms.location.LocationSettingsResponse in project BloodCall by BloodCall.
the class MainActivity method enableLocationSettings.
protected void enableLocationSettings() {
LocationRequest locationRequest = LocationRequest.create().setInterval(LOCATION_UPDATE_INTERVAL).setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
LocationServices.getSettingsClient(this).checkLocationSettings(builder.build()).addOnSuccessListener(this, (LocationSettingsResponse response) -> {
// startUpdatingLocation(...);
}).addOnFailureListener(this, ex -> {
if (ex instanceof ResolvableApiException) {
// Location settings are NOT satisfied, but this can be fixed by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) ex;
resolvable.startResolutionForResult(MainActivity.this, REQUEST_CODE_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
});
}
use of com.google.android.gms.location.LocationSettingsResponse 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;
}
}
});
}
use of com.google.android.gms.location.LocationSettingsResponse in project android-play-location by googlesamples.
the class MainActivity method startLocationUpdates.
/**
* Requests location updates from the FusedLocationApi. Note: we don't call this unless location
* runtime permission has been granted.
*/
private void startLocationUpdates() {
// Begin by checking if the device has the necessary location settings.
mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
Log.i(TAG, "All location settings are satisfied.");
// noinspection MissingPermission
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
updateUI();
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch(statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
Log.i(TAG, "Location settings are not satisfied. Attempting to upgrade " + "location settings ");
try {
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sie) {
Log.i(TAG, "PendingIntent unable to execute request.");
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
String errorMessage = "Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings.";
Log.e(TAG, errorMessage);
Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_LONG).show();
mRequestingLocationUpdates = false;
}
updateUI();
}
});
}
use of com.google.android.gms.location.LocationSettingsResponse in project wiseasily by eFishery.
the class MainActivity method initLocationParameters.
private void initLocationParameters() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
LocationSettingsRequest locationSettingsRequest = builder.build();
Task<LocationSettingsResponse> result = LocationServices.getSettingsClient(this).checkLocationSettings(locationSettingsRequest);
result.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
LocationSettingsResponse response = task.getResult(ApiException.class);
tryToConnect();
} catch (ApiException exception) {
switch(exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvable = (ResolvableApiException) exception;
resolvable.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
Log.d("MainActivity_Location", e.toString());
} catch (ClassCastException e) {
Log.d("MainActivity_Location", e.toString());
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
Log.d("MainActivity_Location", "LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE");
break;
}
}
}
});
}
use of com.google.android.gms.location.LocationSettingsResponse in project NightSkyGuide by MTBehnke.
the class MainActivity method startLocationUpdates.
private void startLocationUpdates() {
// if settings are satisfied initialize location requests
mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
locUpdates = true;
// All location settings are satisfied.
// noinspection MissingPermission - this comment needs to stay here to stop inspection on next line
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
int statusCode = ((ApiException) e).getStatusCode();
switch(statusCode) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// location settings are not satisfied, but this can be fixed 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(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// location settings are not satisfied, however no way to fix the settings so don't show dialog.
Toast.makeText(MainActivity.this, "Location Services Unavailable", Toast.LENGTH_LONG).show();
useGPS = false;
SharedPreferences.Editor edit = preferences.edit();
edit.putBoolean("use_device_location", false);
edit.apply();
break;
}
}
});
}
Aggregations