Search in sources :

Example 1 with SettingsClient

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

Example 2 with SettingsClient

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

Example 3 with SettingsClient

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();
        }
    }
}
Also used : SettingsClient(com.google.android.gms.location.SettingsClient) LocationRequest(com.google.android.gms.location.LocationRequest) LocationSettingsRequest(com.google.android.gms.location.LocationSettingsRequest) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 4 with SettingsClient

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;
            }
        }
    });
}
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)

Aggregations

LocationSettingsRequest (com.google.android.gms.location.LocationSettingsRequest)4 SettingsClient (com.google.android.gms.location.SettingsClient)4 ResolvableApiException (com.google.android.gms.common.api.ResolvableApiException)3 LocationRequest (com.google.android.gms.location.LocationRequest)3 LocationSettingsResponse (com.google.android.gms.location.LocationSettingsResponse)3 ApiException (com.google.android.gms.common.api.ApiException)2 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)2 IntentSender (android.content.IntentSender)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1