private void handleLocationStatusResult(@NonNull Status status) { if (mResolvingError) { // Already attempting to resolve an error. return; } if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) { try { status.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); // TODO: mieux gérer mResolvingError (attendre la résolution) mResolvingError = true; } catch (IntentSender.SendIntentException e) { // There was an error with the resolution intent. Try again. // TODO: mieux notifier le service // mGoogleApiClient.connect(); MainActivity.this.startService(mCollecteServiceIntent); mResolvingError = false; } } else { // TODO: couper l'application ? mResolvingError = true; showErrorDialog(status.getStatusCode()); } }
@Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied -> nothing to do callSuccessCallback(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied. Show the user a dialog to upgrade location settings try { // Show the dialog by calling startResolutionForResult(), and check the result status.startResolutionForResult(mActivity, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { Log.e(TAG, "PendingIntent unable to execute request.", e); callErrorCallback(); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.e(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created."); callErrorCallback(); break; } }
@Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can // initialize location requests here. continueGPSOperation(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied, but this can be fixed // by showing the user a dialog. _result.error("LOCATION DISABLED", "This Android device has it's location disabled", null); 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. _result.error("LOCATION DISABLED", "This Android device has it's location disabled", null); break; } }
@Override public void onConnected(@Nullable Bundle bundle) { LocationRequest locationRequest = createLocationRequest(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); PendingResult<LocationSettingsResult> locationSettingsResultPendingResult = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); locationSettingsResultPendingResult .setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { if (LocationSettingsStatusCodes.SUCCESS != result.getStatus().getStatusCode()) { if (result.getStatus().hasResolution()) { handleLocationStatusResult(result.getStatus()); } else { // TODO: faire quelque chose } } } }); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // la demande des droits est faite ailleurs return; } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); onLocationChanged(LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient)); }
private void configureLocationConnection() { LocationRequest locationRequest = createLocationRequest(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); PendingResult<LocationSettingsResult> locationSettingsResultPendingResult = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); locationSettingsResultPendingResult .setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { if (LocationSettingsStatusCodes.SUCCESS != result.getStatus().getStatusCode()) { Intent localIntent = new Intent(Constants.GOOGLE_API).putExtra(Constants.GOOGLE_API_LOCATION_RESULT, result.getStatus()); LocalBroadcastManager.getInstance(ParcoursService.this).sendBroadcast(localIntent); } } }); // noinspection MissingPermission : permissions dans le manifest LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); }
@Override public void onResult(@NonNull LocationSettingsResult r) { switch (r.getStatus().getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: gpsIsTurnedOn = true; break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { r.getStatus().startResolutionForResult(activity, SideBarActivity.REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException ignored) { ignored.printStackTrace(); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: gpsIsTurnedOn = false; break; } }
/** * GoogleApiClient interfaces */ @Override public void onConnected(@Nullable Bundle bundle) { final LocationRequest locationRequest = LocationRequest.create() .setPriority(LocationRequest.PRIORITY_LOW_POWER); final LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest) .setNeedBle(true); final PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); if (status.getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) { try { status.startResolutionForResult(getCurrentActivity(), NEAR_LOCATION_SETTINGS_CODE); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } } } }); }
@Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: startLocationUpdates(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { status.startResolutionForResult(LocationActivity.this, REQUEST_CHECK_LOCATION_SETTINGS); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); Log.i(TAG, "PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created."); break; } }
@Override public void onResult(@NonNull LocationSettingsResult result) { final Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: mLocationActivityListener.onSettingsCheckSuccess(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { startResolvingSettingsProblem(status); } catch (IntentSender.SendIntentException e) { mLocationActivityListener.onSettingsCheckFailure(); } break; default: mLocationActivityListener.onSettingsCheckFailure(); break; } }
@Override public void onResult(LocationSettingsResult result) { Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: wasSending = JappPreferences.isUpdatingLocationToServer(); if (!wasSending) { showLocationNotification("Japp verzendt je locatie niet!", Color.rgb(244, 66, 66)); } else { showLocationNotification("Japp verzendt je locatie", Color.rgb(113, 244, 66)); } break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: if(listener != null) { listener.onResolutionRequired(status); } break; } }
@Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can // initialize location requests here. startLocationUpdates(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied, but this can be fixed // by showing the user a dialog. 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; } }
@Override public void onResult(LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: Log.i(LOG_TAG, "All location settings are satisfied."); startLocationUpdates(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: Log.i(LOG_TAG, "Location settings are not satisfied. Show the user a dialog to" + "upgrade location settings "); try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { Log.i(LOG_TAG, "PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.i(LOG_TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " + "not created."); break; } }
@Override public void onResult(LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: Log.i(TAG, "All location settings are satisfied."); GetLastLocation(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" + "upgrade location settings "); try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). status.startResolutionForResult(mActivity, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { ErrorHappened("PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: ErrorHappened("Location settings are inadequate, and cannot be fixed here. Dialog " + "not created."); break; } }
@Override protected boolean isLocationEnabled() { if (mLastStatus != null) { if (mLastStatus.hasResolution()) { try { startIntentSenderForResult(mLastStatus.getResolution().getIntentSender(), REQUEST_LOCATION, null, 0, 0, 0, null); } catch (IntentSender.SendIntentException e) { Toast.makeText(getContext(), R.string.err_location_access_unknown_error, Toast.LENGTH_LONG).show(); } } else if (mLastStatus.getStatusCode() == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE) { // no location no party! Toast.makeText(getContext(), R.string.err_location_access_unknown_error, Toast.LENGTH_LONG).show(); } return false; } return true; }
protected boolean isLocationEnabled() { if (mLastStatus != null) { if (mLastStatus.hasResolution()) { try { startIntentSenderForResult(mLastStatus.getResolution().getIntentSender(), REQUEST_LOCATION, null, 0, 0, 0, null); } catch (IntentSender.SendIntentException e) { Toast.makeText(getContext(), R.string.err_location_access_unknown_error, Toast.LENGTH_LONG).show(); } } else if (mLastStatus.getStatusCode() == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE) { // no location no party! Toast.makeText(getContext(), R.string.err_location_access_unknown_error, Toast.LENGTH_LONG).show(); } return false; } return true; }
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; } } }); }
@NonNull private ResultCallback<Result> getLocationSettingsResultCallback() { return new ResultCallback<Result>() { @Override public void onResult(@NonNull Result result) { Status status = result.getStatus(); if (status.getStatusCode() == CommonStatusCodes.SUCCESS) { Log.i(TAG, "All location settings are satisfied."); Runnable requestUpdatesTask = new OnPermissionsTask(); if (isConnected()) requestUpdatesTask.run(); else { mOnConnectedUpdateCallback = requestUpdatesTask; Log.d(TAG, "Google API is not connected. " + "Updates delayed until Google API is connected again."); } } else if (status.getStatusCode() == CommonStatusCodes.RESOLUTION_REQUIRED) { Log.i(TAG, "Location settings are not satisfied. " + "Showing the user a dialog to upgrade location settings."); try { setRequestingPermissions(true); // Show the dialog by calling startResolutionForResult(), // and check the result in the onActivityResult() of mContext status.startResolutionForResult(mContext, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { Log.wtf(TAG, "PendingIntent unable to execute request", e); } } else if (status.getStatusCode() == LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE) { if (checkNetwork()) { Log.w(TAG, "Location settings can't be changed to meet the requirements"); setHasPermissions(false); mOnPermissionsChangedListeners.run(); setRequestingPermissions(false); } } } }; }
private void checkSettingsStatus(Status status) { logger.log(TAG, "checkSettingsStatus -> settings status = " + status); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: logger.log(TAG, "checkSettingsStatus:Location is enabled"); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: logger.log(TAG, "checkSettingsStatus:Location settings are not satisfied. Show the user a dialog to upgrade location settings "); case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: throw new LocationSettingsException(status); } }
@Override @SuppressWarnings({"MissingPermission"}) public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { int statusCode = locationSettingsResult.getStatus().getStatusCode(); if (statusCode == LocationSettingsStatusCodes.SUCCESS) { PendingResult<Status> result = LocationServices.FusedLocationApi.requestLocationUpdates(playServices, locationRequest, this); result.setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { if (status.isSuccess()) { Toast.makeText(getApplicationContext(), "Pedido esta na fila", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Erro ao solicitar location! "+status.getStatusMessage(), Toast.LENGTH_LONG).show(); finish(); } } }); } else if (statusCode == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) { //obter permissao para usar location } else { Toast.makeText(this, "Algum problema ao tentar obter location", Toast.LENGTH_LONG).show(); finish(); } }
private void configureLocationConnection() { LocationRequest locationRequest = createLocationRequest(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); if (collecteActiverPref) { com.google.android.gms.common.api.PendingResult<LocationSettingsResult> locationSettingsResultPendingResult = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); locationSettingsResultPendingResult .setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { if (LocationSettingsStatusCodes.SUCCESS != result.getStatus().getStatusCode()) { Intent localIntent = new Intent(Constants.GOOGLE_API).putExtra(Constants.GOOGLE_API_LOCATION_RESULT, result.getStatus()); LocalBroadcastManager.getInstance(CollecteService.this).sendBroadcast(localIntent); } } }); // noinspection MissingPermission : permissions dans le manifest LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this); //noinspection MissingPermission // mLocationManager.addNmeaListener(new GpsStatus.NmeaListener() { // @Override // public void onNmeaReceived(long l, String s) { // boolean a = false; // } // }); } else { LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); } }
@Override public void onConnected(@Nullable Bundle bundle) { locationRequest=LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(30*1000); locationRequest.setFastestInterval(5*1000); LocationSettingsRequest.Builder builder=new LocationSettingsRequest.Builder() .addLocationRequest(locationRequest); builder.setAlwaysShow(true); locationSettingsResultPendingResult=LocationServices.SettingsApi .checkLocationSettings(mGoogleApiClient,builder.build()); locationSettingsResultPendingResult.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status=locationSettingsResult.getStatus(); final LocationSettingsStates states=locationSettingsResult.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 user a dialog try { status.startResolutionForResult(Gps4Activity.this,REQUEST_LOCATION); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: //Location settings are not satisfied and we have no way to fix the settings //so we cannot show the dialog break; } } }); }
public void requestLocationUpdates() { Log.d(TAG, "requestLocationUpdates()"); LocationServices.SettingsApi.checkLocationSettings(googleApiClient, locationSettingsRequest).setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: Log.d(TAG, "All location settings are satisfied."); startService(new Intent(getApplicationContext(), LocationService.class)); // Expliciting Ignoring this warning, handling it just with a Try-Catch try { LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, LocationService.this); } catch (SecurityException e) { Log.e(TAG, "SecurityException: " + e); } break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: Log.e(TAG, "RESOLUTION_REQUIRED"); // Notify anyone listening for Resolution Broadcasts about the problem to fix. Intent intent = new Intent(ACTION_RESOLUTION); intent.putExtra(EXTRA_STATUS, status); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent); break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.e(TAG, "SETTINGS_CHANGE_UNAVAILABLE"); break; } } }); }
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 if GPS is on or not */ private void isGpsOn() { LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequest); PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: getCurrentLocationAddress(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { status.startResolutionForResult( MapsActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { Log.e(TAG, "Exception : " + e); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.e(TAG, "Location settings are not satisfied."); break; } } }); }
private void enableGPSViaPlayServices() { Crashlytics.log(Log.INFO, MainActivity.class.getSimpleName(), "enableGPSViaPlayServices()"); googleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .build(); googleApiClient.connect(); LocationSettingsRequest locationSettingsRequest = new LocationSettingsRequest.Builder() .addLocationRequest(new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)) .setAlwaysShow(true) .build(); LocationServices.SettingsApi.checkLocationSettings(googleApiClient, locationSettingsRequest) .setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult result) { boolean showingLocationSettingsDialog = false; if (result.getStatus().getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED) { try { //Show location settings change dialog and check the result in onActivityResult() result.getStatus().startResolutionForResult(MainActivity.this, REQUEST_LOCATION); showingLocationSettingsDialog = true; Crashlytics.log(Log.INFO, MainActivity.class.getSimpleName(), "Showing PlayServices GPS settings dialog"); } catch (Exception e) { Crashlytics.log(Log.ERROR, MainActivity.class.getSimpleName(), "Error showing PlayServices GPS settings dialog"); Crashlytics.logException(e); } } if (!showingLocationSettingsDialog) { showNoGPSAlert(); //Ask user to manually enable GPS googleApiClient.disconnect(); } } }); }
/** * 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); } }); }
private void locationSettingsRequest(Context context) { GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context) .addApi(LocationServices.API).build(); googleApiClient.connect(); LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(10000); locationRequest.setFastestInterval(10000/2); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); builder.setAlwaysShow(true); PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: System.out.println("ALL LOCATION SETTINGS ARE SATISFIED"); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { status.startResolutionForResult(getActivity(), 213); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: System.out.println("STUFF"); break; } } }); }
public void startLocationUpdates() { LocationServices.SettingsApi.checkLocationSettings( mGoogleClient, mLocationSettingsRequest ).setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: requestLocation(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { // Show the dialog by calling startResolutionForResult(), and check the // result in onActivityResult(). status.startResolutionForResult( activity, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException ignored) {} break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Toast.makeText(activity, R.string.string_location_settings_inadequate, Toast.LENGTH_LONG).show(); } } }); }
private void startLocationUpdates() { Log.i(TAG, "startLocationUpdates"); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequest); // 現在位置の取得の前に位置情報の設定が有効になっているか確認する PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // 設定が有効になっているので現在位置を取得する if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, MainActivity.this); } break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // 設定が有効になっていないのでダイアログを表示する try { status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.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; } } }); }
/** * The callback invoked when * {@link com.google.android.gms.location.SettingsApi#checkLocationSettings(GoogleApiClient, * LocationSettingsRequest)} is called. Examines the * {@link com.google.android.gms.location.LocationSettingsResult} object and determines if * location settings are adequate. If they are not, begins the process of presenting a location * settings dialog to the user. */ @Override public void onResult(LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: Log.i(this.getLocalClassName(), "All location settings are satisfied."); // startLocationUpdates(); requestingLocationUpdates = true; break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: Log.i(this.getLocalClassName(), "Location settings are not satisfied. Show the user a dialog to " + "upgrade location settings "); try { // Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult(). status.startResolutionForResult(TabbedListActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { //TODO Log.i(this.getLocalClassName(), "PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: //TODO Log.i(this.getLocalClassName(), "Location settings are inadequate, and cannot be fixed here. Dialog " + "not created."); break; } }
@Override public void onResult(LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: logger.d("All location settings are satisfied."); fulfilledCheckLocationSettings = true; startUpdating(locationRequest); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: logger.w("Location settings are not satisfied. Show the user a dialog to" + "upgrade location settings. You should hook into the Activity onActivityResult and call this provider onActivityResult method for continuing this call flow. "); if (context instanceof Activity) { try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). status.startResolutionForResult((Activity) context, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { logger.i("PendingIntent unable to execute request."); } } else { logger.w("Provided context is not the context of an activity, therefore we cant launch the resolution activity."); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: logger.i("Location settings are inadequate, and cannot be fixed here. Dialog " + "not created."); stop(); break; } }
@Override public void onResult(LocationSettingsResult result) { // Get status final Status status = result.getStatus(); Log.i(this.getClass().getSimpleName(), "LocationSettingsResult is: " + status.getStatusMessage()); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. Initialize location updates request. if (!mUpdatingLocation) { this.startLocationUpdates(); } break; 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(). mResolvingSettings = true; status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.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. // TODO explain to user break; } }
private void createLocationRequestForResult(){ mLocationRequestBalancedPowerAccuracy = new LocationRequest(); final LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .addLocationRequest(mLocationRequestBalancedPowerAccuracy) .setAlwaysShow(true); PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult locationSettingsResult) { Log.v("BLE", locationSettingsResult.getStatus().getStatusMessage()); LocationSettingsStates states = locationSettingsResult.getLocationSettingsStates(); if(states.isLocationUsable()) { checkForLocationPermissionsAndScan(); return; } final Status status = locationSettingsResult.getStatus(); switch(status.getStatusCode()){ case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: mLocationServicesRequestApproved = false; try { status.startResolutionForResult(MainActivity.this, REQUEST_LOCATION_SERVICES); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } break; case LocationSettingsStatusCodes.SUCCESS: mLocationServicesRequestApproved = true; checkForLocationPermissionsAndScan(); break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: showPermissionRationaleFragment(R.string.rationale_location_cancel_message, 0); break; } } }); }
private void handleLocationSettings(final ResourceObserver<T> emitter, final GoogleApiClient client) { if (activityResultCanceled(STATE_NAME)) { emitter.onError(new LocationSettingDeniedException()); return; } LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder() .setAlwaysShow(true) .addLocationRequest(locationRequest); PendingResult<LocationSettingsResult> pendingResult = LocationServices.SettingsApi.checkLocationSettings(client, builder.build()); pendingResult.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(LocationSettingsResult result) { final Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: locationSettingSuccess(emitter, client); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: resolveResolutionRequired(emitter, status); emitter.onComplete(); // silently close our api connection, full restart through responsehandler required, so we can close this connection. break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: emitter.onError(new RxPlayServices.RxLocationError(LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE)); break; } } }); }
@Override public void onResult(LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // NO need to show the dialog; break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied. Show the user a dialog try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { //unable to execute request } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: // Location settings are inadequate, and cannot be fixed here. Dialog not created break; } }
/** * The callback invoked when * {@link com.google.android.gms.location.SettingsApi#checkLocationSettings(GoogleApiClient, * LocationSettingsRequest)} is called. Examines the * {@link com.google.android.gms.location.LocationSettingsResult} object and determines if * location settings are adequate. If they are not, begins the process of presenting a location * settings dialog to the user. */ @Override public void onResult(LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: Log.i(TAG, "All location settings are satisfied."); startLocationUpdates(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to" + "upgrade location settings "); try { // Show the dialog by calling startResolutionForResult(), and check the result // in onActivityResult(). status.startResolutionForResult(FusedLocationManager.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { Log.i(TAG, "PendingIntent unable to execute request."); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog " + "not created."); break; } }
@Override public void onResult(LocationSettingsResult locationSettingsResult) { final Status status = locationSettingsResult.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: conectadoLocation = true; detenerTareas(); paradasCercanas.seleccionarProximidad(); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { status.startResolutionForResult(MapasActivity.this, REQUEST_CHECK_SETTINGS); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); conectadoLocation = false; } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: conectadoLocation = false; break; } }
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; } } }); }
@Override protected void onGoogleApiClientReady(GoogleApiClient apiClient, final SingleEmitter<Boolean> emitter) { emitterWeakRef = new WeakReference<>(emitter); setupLocationPendingResult( LocationServices.SettingsApi.checkLocationSettings(apiClient, locationSettingsRequest), new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult result) { Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: // All location settings are satisfied. The client can initialize location // requests here. emitter.onSuccess(true); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: // Location settings are not satisfied. But could be fixed by showing the user // a dialog. if (context != null) { String observableId = UUID.randomUUID().toString(); observableMap.put(observableId, new WeakReference<>(SettingsCheckHandleSingleOnSubscribe.this)); Intent intent = new Intent(context, LocationSettingsActivity.class); intent.putExtra(LocationSettingsActivity.ARG_STATUS, status); intent.putExtra(LocationSettingsActivity.ARG_ID, observableId); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { emitter.onSuccess(false); } 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. emitter.onSuccess(false); break; default: emitter.onError(new StatusException(status)); break; } } } ); }