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()); } }) // if settings need to be changed prompt user .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; } } }); }
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); } }); }
public 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()); } }) // if settings need to be changed prompt user .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(SettingsActivity.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(context, "Location Services Unavailable", Toast.LENGTH_LONG).show(); useGPS = false; SharedPreferences.Editor edit = preferences.edit(); edit.putBoolean("use_device_location", false); edit.apply(); settingsFragment.useDeviceLocation.setChecked(false); settingsFragment.setLocSummary(); break; } } }); }
/** * 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>() { @SuppressLint({"MissingPermission", "StaticFieldLeak"}) @Override public void onSuccess(LocationSettingsResponse locationSettingsResponse) { Log.i(TAG, "All location settings are satisfied."); mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); updateUI(null); } }) .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 " + ((ApiException) e).getStatusMessage()); try { // Show the dialog by calling startResolutionForResult(), and check the // result in onActivityResult(). ResolvableApiException rae = (ResolvableApiException) e; rae.startResolutionForResult(MapActivity.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(MapActivity.this, errorMessage, Toast.LENGTH_LONG).show(); mRequestingLocationUpdates = false; } updateUI(null); } }); }
@NonNull public Single<LocationSettingsResponse> checkLocationSettings(LocationSettingsRequest request) { return SingleTask.create(() -> client.checkLocationSettings(request)); }
private void checkLocationSettings() { Log.d(TAG, "Checking device's configuration"); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder(); builder.addLocationRequest(mLocationRequest); LocationSettingsRequest mLocationSettingsRequest = builder.build(); mSettingsClient.checkLocationSettings(mLocationSettingsRequest) .addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() { @Override public void onSuccess(LocationSettingsResponse locationSettingsResponse) { Log.d(TAG, "All location settings are satisfied"); findCurrentLocation(); } }) .addOnFailureListener(this, new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { int statusCode = ((ApiException) e).getStatusCode(); switch (statusCode) { case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: Log.d(TAG, "Location settings are not satisfied. Attempting to " + "upgrade location settings"); try { ResolvableApiException rae = (ResolvableApiException) e; rae.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS); if (mResultCallBack != null) mResultCallBack.onConfigurationNeeded(); } catch (IntentSender.SendIntentException sie) { Log.d(TAG, "PendingIntent unable to execute request."); if (mResultCallBack != null) mResultCallBack.onError(sie.getMessage()); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: String errorMessage = "Location settings are inadequate, " + "and cannot be fixed here. Fix in Settings."; Log.d(TAG, errorMessage); if (mResultCallBack != null) mResultCallBack.onError(errorMessage); } } }); }
protected void startLocationUpdates() { //first check to see if I have permissions (marshmallow) if I don't then ask, otherwise start up the demo. if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { //I'm on not explaining why, just asking for permission. Log.v(TAG, "asking for permissions"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, MainActivity.REQUEST_ACCESS_startLocationUpdates); return; } // Begin by checking if the device has the necessary location settings. mSettingsClient.checkLocationSettings(mLocationSettingsRequest) .addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() { @SuppressLint("MissingPermission") @Override public void onSuccess(LocationSettingsResponse locationSettingsResponse) { Log.i(TAG, "All location settings are satisfied."); //noinspection MissingPermission 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: 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; } } }); }
protected void startLocationUpdates() { //first check to see if I have permissions (marshmallow) if I don't then ask, otherwise start up the demo. if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { //I'm on not explaining why, just asking for permission. Log.v(TAG, "asking for permissions"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, MainActivity.REQUEST_ACCESS_startLocationUpdates); return; } // Begin by checking if the device has the necessary location settings. mSettingsClient.checkLocationSettings(mLocationSettingsRequest) .addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() { @SuppressLint("MissingPermission") @Override public void onSuccess(LocationSettingsResponse locationSettingsResponse) { Log.i(TAG, "All location settings are satisfied."); //noinspection MissingPermission mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); } }) .addOnFailureListener(this, new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { mRequestingLocationUpdates = false; int statusCode = ((ApiException) e).getStatusCode(); switch (statusCode) { case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: Log.i(TAG, "Location settings are not satisfied. "); 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; } } }); }
/** * 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(); } }); }