Search in sources :

Example 6 with LocationSettingsResponse

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.
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) IntentSender(android.content.IntentSender)

Example 7 with LocationSettingsResponse

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;
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException) SettingsClient(com.google.android.gms.location.SettingsClient) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 8 with LocationSettingsResponse

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();
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 9 with LocationSettingsResponse

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;
                }
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) IntentSender(android.content.IntentSender) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Example 10 with LocationSettingsResponse

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;
            }
        }
    });
}
Also used : ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) LocationSettingsResponse(com.google.android.gms.location.LocationSettingsResponse) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener) ResolvableApiException(com.google.android.gms.common.api.ResolvableApiException) ApiException(com.google.android.gms.common.api.ApiException)

Aggregations

ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)12 LocationSettingsResponse (com.google.android.gms.location.LocationSettingsResponse)12 ApiException (com.google.android.gms.common.api.ApiException)11 LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)8 LocationRequest (com.google.android.gms.location.LocationRequest)7 IntentSender (android.content.IntentSender)6 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)5 SettingsClient (com.google.android.gms.location.SettingsClient)3 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)3 Activity (android.app.Activity)1 PendingIntent (android.app.PendingIntent)1 Context (android.content.Context)1 FusedLocationProviderClient (com.google.android.gms.location.FusedLocationProviderClient)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1