maya
maya

Reputation: 159

How to track if user has intentionally turned off location, using Fused Location Provider API

I have used the Fused Location Provider API to get user's current location.Things work well when the user has turned location on.

But I want to provide a message to the user if he has turned location off. When location is turned off, the requestLocationUpdates does not call onLocationChanged method . May be this is expected, but i would like to know what is called when location is turned off so that i can capture it to inform the user. Below is the code I am using (picked from an example in Stackoverflow, the link to which is lost from me).

public class FusedLocationService implements
    LocationListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final String TAG = "FusedLocationService";

private static final long INTERVAL = 1000 * 30;
private static final long FASTEST_INTERVAL = 1000 * 5;
private static final long ONE_MIN = 1000 * 60;
private static final long REFRESH_TIME = ONE_MIN * 5;
private static final float MINIMUM_ACCURACY = 50.0f;
Activity locationActivity;
private LocationRequest locationRequest;
private GoogleApiClient googleApiClient;
private Location location;
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;

FusedLocationReceiver locationReceiver = null;

public FusedLocationService(Activity locationActivity, FusedLocationReceiver locationReceiver) {
    this.locationReceiver = locationReceiver;
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(INTERVAL);
    locationRequest.setFastestInterval(FASTEST_INTERVAL);
    this.locationActivity = locationActivity;

    googleApiClient = new GoogleApiClient.Builder(locationActivity)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    if (googleApiClient != null) {
        googleApiClient.connect();
    }
}

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "I'm connected now");
    Location currentLocation = fusedLocationProviderApi.getLastLocation(googleApiClient);
    if (currentLocation != null && currentLocation.getTime() > REFRESH_TIME) {
        location = currentLocation;
    } else {
        fusedLocationProviderApi.requestLocationUpdates(googleApiClient, locationRequest, this);
        // Schedule a Thread to unregister location listeners
        Executors.newScheduledThreadPool(1).schedule(new Runnable() {
            @Override
            public void run() {
                fusedLocationProviderApi.removeLocationUpdates(googleApiClient,
                        FusedLocationService.this);
            }
        }, ONE_MIN, TimeUnit.MILLISECONDS);
    }
}

@Override
public void onLocationChanged(Location location) {
    Log.i(TAG, "Location is changed!");
    //if the existing location is empty or
    //the current location accuracy is greater than existing accuracy
    //then store the current location
    if (null == this.location || location.getAccuracy() < this.location.getAccuracy()) {
        this.location = location;
// let's inform my client class through the receiver
        locationReceiver.onLocationChanged();
        //if the accuracy is not better, remove all location updates for this listener
        if (this.location.getAccuracy() < MINIMUM_ACCURACY) {
            fusedLocationProviderApi.removeLocationUpdates(googleApiClient, this);
        }
    }
}

public Location getLocation() {
    return this.location;
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

   }

}

Upvotes: 3

Views: 1890

Answers (2)

Vishnu Prabhu
Vishnu Prabhu

Reputation: 459

Please go through the SettingApi, which you can use to display dialog.

https://developer.android.com/reference/com/google/android/gms/location/SettingsApi.html

Skim Location settings dialog. Here you can find what you have to do with the dialog.

http://android-developers.blogspot.in/2015/03/google-play-services-70-places-everyone.html

Upvotes: 0

Andrew Prock
Andrew Prock

Reputation: 7127

I think you can just use the LocationManager per this question: How to check if Location Services are enabled?

Upvotes: 1

Related Questions